简体   繁体   中英

Filter LINQ to entities query based on 2 dimensional array

This is a Linq2entities challenge...

I have an entity (ID, CategoryID, Value) and a 2 dimensional int array with CategoryID / Value pairs. I need to get all entities filtered by each pair, something like:

from e in Entity
where (e.CategoryID and e.Value) in array
select e;

So basically is a "two linked fields" filter.

A dirty solution would be to concat and compare, like:

concatarray = some function to concat CategoryID + "/" + Value; 

from e in Entity
where e.CategoryID + "/" + e.Value in concatarray
select e;

but I don't want to use this because of performance issues.

Any idea?

Thanks a lot!

First, I would convert your array into list of objects with specific properties. Using 2d array for this is not good idea.

Then the query, might not be translatable to SQL in EF.

from e in Entity
where array.Where(a=>a.CategoryID == e.CategoryID && a.Value == e.Value).Any()
select e

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM