2011-05-12 53 views
0

這是一個挑戰Linq2entities ...過濾LINQ到實體查詢根據2維陣列

我有一個實體(ID,類別ID,值),並與類別ID /值對的2維int數組。我需要通過每對過濾所有的實體,是這樣的:

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

所以基本上是「兩分田」過濾器。

一個骯髒的解決辦法是Concat的和比較,如:

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

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

,但我不希望使用的,因爲性能問題這一點。

有什麼想法?

非常感謝!

回答

3

首先,我會將你的數組轉換成具有特定屬性的對象列表。使用2d數組不是個好主意。

然後查詢可能無法在EF中轉換爲SQL。

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

+1但爲了簡潔,首選'.Any(a => a.CategoryID == e.CategoryID && a.Value == e.Value)'。 「 – 2011-05-12 06:35:34

+0

沒有工作人員: 」無法創建類型爲'GEO.Data.CategoryAttributeValue'的常量值。「僅限基本類型...」 List attributes = new List (); attributes.Add(new CategoryAttributeValue {CategoryAttributeID = 1,Value = 1}); var queryAttributes = from a in db.CategoryAttributeValues where attributes.Where(av => av.CategoryAttributeID == a.CategoryAttributeID).Any() select a; queryAttributes.ToList(); – Milox 2011-05-13 02:01:29