2012-04-19 63 views
1

我要簡化分貝。
這裏是什麼樣子:怎麼做到的(簡單嗎?)的LINQ查詢

表:

Main: Products: ID, Ref, Price, 
Translation: ID, Product_ID, Language_ID, Name 
Images: ID, Product_ID, Path, Index 

我新手與LINQ和我試過檢索所有產品,他們的名字與LANGUAGE_ID = 1,式中指數= 1

From p In db.Products 
Join t In db.Translate_Products On p.ID_Product Equals t.Product_ID 
Join i In db.Images On p.ID_Product Equals i.Product_ID 
Where t.Language_ID = 1 And i.Index= 0 
Select p, t, i 
+4

你應該問_ 「我怎麼接受的答案」 _! ** 0滿分10 **令人印象深刻! – gdoron 2012-04-19 13:37:23

+2

13個問題,0接受,你可能想考慮閱讀StackExchange的工作方式 – 2012-04-19 13:37:48

+0

你是什麼意思? – blop 2012-04-19 13:38:27

回答

2
From p In db.Products 
Join t In db.Translate_Products On p.ID_Product equals t.Product_ID into results1 
from r1 in results.DefaultIfEmpty() 
Join i In db.Images On p.ID_Product equals i.Product_ID 
into results2 
from r2 in results2.DefaultIfEmpty() 
Where results.Language_ID = 1 And i.Index= 0 
Select new 
{ 

Productid = p.Productid, 
.. 
.. 
.. 
} 
+0

其實我不能得到它的工作... Visual Studio的犯規讓我有一個「進」,在第二行。注意一定要了解它是如何工作既不... – blop 2012-04-20 08:47:07

+0

成是LINQ有效驗證..如果p.ID_Product和t.Product_ID是同一類型的。而共享你所得到的錯誤消息.. – 2012-04-20 08:55:47

+0

Visual Studio中說(它強調了吉爾)「excpected指令結束」 – blop 2012-04-20 09:05:21

0
From p In db.Products 
Join t In db.Translate_Products On p.ID_Product equals t.Product_ID 
into results1 
from r1 in results1.DefaultIfEmpty() 
Join i In db.Images On p.ID_Product equals i.Product_ID  
Where t.Language_ID == 1 And i.Index== 0 
Select new 
{ 

Productid = p.Productid, 
.. 
.. 
.. 
} 
+0

感謝名單,但仍然沒有工作......只是寫從p在db.Products加入噸db.Translate_Products在p.ID_Product的Equals t.Product_ID進入results1給我一個錯誤...是不是vb? – blop 2012-04-20 09:46:55

+0

它是C#代碼..我使用的是相同的查詢,但不同的表和它的作品對我來說非常好....不知道你的情況的好友.. – 2012-04-20 09:55:38

+0

我用vb ...是什麼問題?我認爲在VB和C#中linq是一樣的...... – blop 2012-04-20 10:05:31