2012-06-06 41 views
0
Product 
+------------+-------------+------------+ 
| PID  | Name  | Model  | 
+------------+-------------+------------+ 
|   1 | Mercedes | xyz  | 
|   2 | Audi  | yxz  | 
|   3 | BMW   | zyx  | 
+------------+-------------+------------+ 


ProductColor 
+------------+-------------+------------+ 
| ID   | ProductID | Color  | 
+------------+-------------+------------+ 
|   1 | 1   | Red  | 
|   2 | 1   | Blue  | 
|   3 | 3   | Blue  | 
+------------+-------------+------------+ 

我需要從ProductColor中選擇產品ID,其中藍色和紅色?如何編寫正確的Linq查詢?Linq加入一對多AND標準

+0

你到目前爲止嘗試過什麼?你能提供你的課程的例子嗎? – Richard

+0

[我如何檢索用linq中所有提供的標籤標記的項目?](http://stackoverflow.com/questions/3478874/how-do-i-retrieve-items-that-are-tagged (x => x.ProductColors.Where(y => y.Color ==「blue」)。選擇(所有提供的標籤在LINQ) –

回答

0

假設你對數據庫表的關係,這樣的事情應該工作:

C#:

var ProductIDs = ctx.Products.Where(p => p.Color.Contains("Red") && 
    p.Color.Contains("Blue")).Select(p => p.PID) 

VB:

Dim ProductIDs = ctx.Products.Where(function(p) p.Color.Contains("Red") And 
    p.Color.Contains("Blue")).Select(function(p) p.PID) 
+0

列表 Pr = data.Products.Where (z => z.ProductID).Contains(x.ID)&& x.ProductColors.Where(y => y.Color ==「red」)。Select(z => z.ProductID).Contains(x.ID) ).ToList();如何預測Builder多標準? – user1439338

0
List<Product> Pr= data.Products.Where(x=> x.ProductColors.Where(y=> 
y.Color=="blue").Select(z=> z.ProductID).Contains(x.ID) && 
x.ProductColors.Where(y=> y.Color== "red").Select(z=> 
z.ProductID).Contains(x.ID)).ToList() 

綠色
黃色
n

如何使用多標準linq查詢?