views:

32

answers:

2

I am trying to accomplish something like this query:

var query = from a in DatabaseTable
            where listOfObjects.Any(x => x.Id == a.Id)
            select a;

Basically, I want to filter the results where a.Id equals a property of one of the objects in the generic list "listOfObjects". I'm getting the error "Local sequence cannot be used in LINQ to SQL implementation of query operators except the Contains() operator."

Any ideas on how to filter this in an easily readable way using "contains" or another method?

Thanks in advance.

+3  A: 

Just project your local list into a list of the specific items you need to filter on:

var listOfIds = listOfObjects.Select(o => o.Id);
var query = 
    from a in DatabaseTable
    where listOfIds.Contains(a.Id)
    select a;
Aaronaught
Aww... I was too late. :)
Stephen Cleary
Gah...so obvious. Thanks I'm new to LINQ.
Ocelot20
+2  A: 
var listOfIds = listOfObjects.Select(x => x.Id).ToList();

var query = from a in DatabaseTable
            where listOfIds.Contains(a.Id)
            select a;
Stephen Cleary