Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- List<string> customersTitles = Odata.OrdersService.Select(o=>o.CustomerTitle).Distinct().ToList();
- List<Customer> customers = Odata.CustomerService.Where(m => customersTitles .Contains(m.CustomerTitle))
- public class CustomerController : EntitySetController<Customer, int>
- {
- [Queryable]
- public override IQueryable<Customer> Get()
- {
- Expression filter = this.QueryOptions.Filter.ToExpression<Customer>();
- return db.Query<Customer>(filter as Expression<Func<Customer, bool>>);
- }
- }
- List<Customer> customers = Odata.CustomerService.Where(m => customersTitles .Contains(m.CustomerTitle))
- List<Customer> customers = Odata.CustomerService.Where(m => m.CustomerTitle == customerTitles[0] || m.CustomerTitle == customerTitles[1]); // and so on
- var titleFilterList = customerTitles.Select(title => String.Format("(CustomerTitle eq {0})", title));
- var titleFilter = String.Join(" or ", titleFilterList);
- var customers = Odata.CustomerService.AddQueryOption("$filter", titleFilter).Execute().ToList(); // you may have to cast this.
- public static IQueryable<T> WherePropertyIsIn<T, TSet>(
- this IQueryable<T> query,
- IEnumerable<TSet> set,
- Expression<Func<T, TSet>> propertyExpression
- ) {
- var filterPredicate = set.Select(value => Expression.Equal(propertyExpression.Body, Expression.Constant(value)))
- .Aggregate<Expression, Expression>(Expression.Constant(false), Expression.Or);
- var filterLambdaExpression = Expression.Lambda<Func<T, bool>>(filterPredicate, propertyExpression.Parameters.Single());
- return query.Where(filterLambdaExpression);
- }
- var allowed_states = getAllowedStates();
- var maxPopulation = getMaxPopulation();
- // Instead of...
- var cities = context.Cities.Where(c => allowed_states.Contains(c.State) && c.Population <= maxPopulation);
- // Use...
- 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