LINQ Query with Array input and variable Where Statements - Advice

前端 未结 3 1476
予麋鹿
予麋鹿 2021-01-20 03:25

I would like to query data given an array to filter by via WCF Data Services using the Silverlight Client API. Basically, I want to query Employees given a list (array) of

3条回答
  •  盖世英雄少女心
    2021-01-20 03:57

    You can dynamically build the expression tree for the condition.

    var parameter = Expression.Parameter(typeof(Employee), "employee");
    
    Expression condition = Expression.Constant(false);
    
    foreach (var state in states)
    {
        condition = Expression.OrElse(
            condition,
            Expression.Equal(
                Expression.Property(parameter, "State"),
                Expression.Constant(state)));
    }
    
    var expression = Expression.Lambda>(condition, parameter);
    

    And then just perform the call.

    var result = Context.Employees.Where(expression);
    

    I am not 100% sure if this will work out of the box for you but I hope the general idea helps.

提交回复
热议问题