开发者

Mixing LINQ to SQL with properties of objects in a generic list

开发者 https://www.devze.com 2022-12-30 14:08 出处:网络
I am trying to accomplish something like this query: var query = from a in DatabaseTable where listOfObjects.Any(x => x.Id == a.Id)

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 filt开发者_运维知识库er this in an easily readable way using "contains" or another method?

Thanks in advance.


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;


var listOfIds = listOfObjects.Select(x => x.Id).ToList();

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号