Advertisement
Guest User

Untitled

a guest
Oct 21st, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. public Expression<Func<Purchase, bool>> forTarget(List<string> idList)
  2. {
  3. Expression<Func<Purchase, string>> fc = p => p.ClientId;
  4. Expression<Func<Purchase, bool>> predicate = m => false;
  5.  
  6. return idList.Aggregate(predicate, (p, id) => p.Or(m => fc.Invoke(m) == id), p => p.Expand());
  7. }
  8.  
  9. internal class Purchase
  10. {
  11. public int Price { get; set; }
  12. public string Description { get; set; }
  13. public string ClientId { get; set; }
  14. }
  15.  
  16. public class Client
  17. {
  18. public string Id { get; set; }
  19. }
  20.  
  21. return idList.Aggregate(predicate,
  22. (p, id) => p.Or(m => fc.Invoke(m) == id),
  23. p => p.Expand());
  24.  
  25. IEnumerable<Purchase> purchases = LoadSelectedItems();
  26. var clientIds = purchases.Select( p => p.ClientId ).ToArray();
  27. var results = db.Clients.Where( c => clientIds.Contains( c.Id )); // Did not work.
  28.  
  29. var results = db.Clients.Where( c => c.Id == 1 || c.Id == 2 || c.Id == 3);
  30.  
  31. // start with a predicate returning false
  32. // this is the seed of the Aggregate method
  33. Expression<Func<Purchase, bool>> predicate = m => false;
  34. // Now, iterate the collection and build the full predicate
  35. foreach( var id in idList)
  36. {
  37. // Build the predicate by invoking a function which returns the client id of the
  38. // purchase and comparing it with the value of the current id from the idList
  39. predicate = predicate.Or(item => item.ClientId == id);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement