vb.net 在 linq 中使用左连接查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13496710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Query with left join in linq
提问by blop
Hello I 'm trying to do a query in linq but I think I need some help...
您好,我正在尝试在 linq 中进行查询,但我想我需要一些帮助...
I have two table: langAvailable and Translations I want a 'line' even if theres no translation recorded
我有两个表:langAvailable 和 Translations 我想要一个“行”,即使没有记录翻译
For instance:
例如:
LangId: 1 TranslationID:10 Translation: Hello
LangId: 2 TranslationID:10 Translation: Bonjour
LangId: 1 TranslationID:11 Translation: Thanks
LangId: 2 TranslationID:11 Translation:
Here's what I do:
这是我所做的:
Dim query = From c In db.LangAvailable _
Join o In db.Translate On c.ID_Lang Equals o.Lang_ID _
Where o.TranslationID = 243 _
Select New With {c.LangId, o.Translation}
This only give me one record if there s no translation in the translate table... Any idea how I can get that?
如果翻译表中没有翻译,这只会给我一个记录......知道我怎么能得到吗?
Thanx
谢谢
回答by HotN
You can use Group Join to do what you're trying to do. Try this:
您可以使用 Group Join 来做您想做的事情。尝试这个:
Dim query = From c In db.LangAvailable _
Group Join o In db.Translate On c.ID_Lang Equals o.Lang_ID Into Group _
From o In Group.DefaultIfEmpty() _
Where o.TranslationId = 243 _
Select LangId = c.ID_Lang, Translation = If(o.Translation Is Nothing, Nothing, o.Translation)

