Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. List<string> customersTitles = Odata.OrdersService.Select(o=>o.CustomerTitle).Distinct().ToList();
  2.  
  3. List<Customer> customers = Odata.CustomerService.Where(m => customersTitles .Contains(m.CustomerTitle))
  4.  
  5. public class CustomerController : EntitySetController<Customer, int>
  6. {
  7.  
  8. [Queryable]
  9. public override IQueryable<Customer> Get()
  10. {
  11. Expression filter = this.QueryOptions.Filter.ToExpression<Customer>();
  12. return db.Query<Customer>(filter as Expression<Func<Customer, bool>>);
  13. }
  14. }
  15.  
  16. List<Customer> customers = Odata.CustomerService.Where(m => customersTitles .Contains(m.CustomerTitle))
  17.  
  18. List<Customer> customers = Odata.CustomerService.Where(m => m.CustomerTitle == customerTitles[0] || m.CustomerTitle == customerTitles[1]); // and so on
  19.  
  20. var titleFilterList = customerTitles.Select(title => String.Format("(CustomerTitle eq {0})", title));
  21. var titleFilter = String.Join(" or ", titleFilterList);
  22. var customers = Odata.CustomerService.AddQueryOption("$filter", titleFilter).Execute().ToList(); // you may have to cast this.
  23.  
  24. public static IQueryable<T> WherePropertyIsIn<T, TSet>(
  25. this IQueryable<T> query,
  26. IEnumerable<TSet> set,
  27. Expression<Func<T, TSet>> propertyExpression
  28. ) {
  29. var filterPredicate = set.Select(value => Expression.Equal(propertyExpression.Body, Expression.Constant(value)))
  30. .Aggregate<Expression, Expression>(Expression.Constant(false), Expression.Or);
  31.  
  32. var filterLambdaExpression = Expression.Lambda<Func<T, bool>>(filterPredicate, propertyExpression.Parameters.Single());
  33.  
  34. return query.Where(filterLambdaExpression);
  35. }
  36.  
  37. var allowed_states = getAllowedStates();
  38. var maxPopulation = getMaxPopulation();
  39.  
  40. // Instead of...
  41. var cities = context.Cities.Where(c => allowed_states.Contains(c.State) && c.Population <= maxPopulation);
  42.  
  43. // Use...
  44. var cities = context.Cities.Where(c => c.Population <= maxPopulation).WherePropertyIsIn(allowed_states, c => c.Cities);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement