3

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 States.

I'm thinking something like this:

public IQueryable<Employee> Load(string[] states)
{
     foreach (var x in states)
     {
           // LINQ query here with 1 to N .Where statements
           return from e in Context.Employees
           .Where(...)
     }
} 

So let's say my array has 2 items in it, i.e. I want to query by 2 states, I would do something like this manually:

return from e in Context.Employees
    .Where(e => e.State== states[0] || e.State == states[1])));

Any advice will be greatly appreciated!

2
  • Are you using LINQ to SQL or entity framework or plain LINQ? Commented Jul 15, 2010 at 19:12
  • Edited original post - using WCF Data Services (Astoria 1.0) with Silverlight 3.0 (Silverlight Client libraries). Commented Jul 15, 2010 at 19:22

3 Answers 3

5

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<Func<Employee, Boolean>>(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.

Sign up to request clarification or add additional context in comments.

3 Comments

Probably not Or but OrElse. And Parameter(typeof(Employee)...
Thanks, you are right - I wrote the code using a list of String for testing and forgot to change it to Employee. The difference between Or and OrElse should not matter but one would of course usually use a conditional or instead of the normal or.
Good comment @DanielBrückner but Equal method makes a case-sensitive comparison, instead how to perform a case-insensitive comparison? Expression.Contains() method does not exists.
1

Context.Employees.ToList().Where(x => states.Contains(x.State))

8 Comments

This will not work with EntityFramework or LINQ to SQL. I assume one if this is used because the parameter is IQeryable and not IEnumerable.
it should do if you call tolist() before the where.
You may want to consider performance if this is a large table though as all employees would be read in, then the filter applied
He will have to test it but I am quite sure that neither EF nor L2S is capable of translating a complex object like a list into a matching SQL statement - at least prior to .NET 4.0.
your right(I think), but tolist converts it into a normal list object, so even if ef or l2s don't support it you should be dealing with L2o at that point. The query has already been executed on the db when running the filter. I would test it without tolist first as that would be more efficient if it worked.
|
0

Here's a runnable example that does what you want, I think? Given a list of states, it will give you the employees that are in those states.

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> states = new List<string>();
            states.Add("SC");
            states.Add("TX");
            states.Add("NC");

            List<Employee> emps = new List<Employee>();
            emps.Add(new Employee() { State = "GA", Name = "Bill" });
            emps.Add(new Employee() { State = "TX", Name = "John" });
            emps.Add(new Employee() { State = "SC", Name = "Mary" });

            //Here's where the work is done.  The rest is fluff...
            var empsinstates = from e in emps where states.Contains(e.State) select e;

            foreach (var e in empsinstates)
            {
                Console.WriteLine(e.Name + " " + e.State);
            }
            Console.Read();
        }
    }
    class Employee
    {
        public string State;
        public string Name;
    }
}

2 Comments

This works with LINQ to Object but (quite surely) not with Entity Framework or LINQ to SQL.
This is what I am getting: {Error translating Linq expression to URI: The method 'Contains' is not supported.}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.