Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Expression<Func<Purchase, bool>> forTarget(List<string> idList)
- {
- Expression<Func<Purchase, string>> fc = p => p.ClientId;
- Expression<Func<Purchase, bool>> predicate = m => false;
- return idList.Aggregate(predicate, (p, id) => p.Or(m => fc.Invoke(m) == id), p => p.Expand());
- }
- internal class Purchase
- {
- public int Price { get; set; }
- public string Description { get; set; }
- public string ClientId { get; set; }
- }
- public class Client
- {
- public string Id { get; set; }
- }
- return idList.Aggregate(predicate,
- (p, id) => p.Or(m => fc.Invoke(m) == id),
- p => p.Expand());
- IEnumerable<Purchase> purchases = LoadSelectedItems();
- var clientIds = purchases.Select( p => p.ClientId ).ToArray();
- var results = db.Clients.Where( c => clientIds.Contains( c.Id )); // Did not work.
- var results = db.Clients.Where( c => c.Id == 1 || c.Id == 2 || c.Id == 3);
- // start with a predicate returning false
- // this is the seed of the Aggregate method
- Expression<Func<Purchase, bool>> predicate = m => false;
- // Now, iterate the collection and build the full predicate
- foreach( var id in idList)
- {
- // Build the predicate by invoking a function which returns the client id of the
- // purchase and comparing it with the value of the current id from the idList
- predicate = predicate.Or(item => item.ClientId == id);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement