ResourceTranslation
--------------------------------------------
ID binary(16) |
ShortStringResourceID binary(16) |
LocaleName varchar(50) |
TranslatedText text |
--------------------------------------------
ShortStringresource
--------------------------------------------
ID binary(16) |
PrimaryLocaleName varchar(50) |
ContentText varchar(255) |
--------------------------------------------
我想在 LINQ to SQL 中实现与以下 SQL 等效的功能:
SELECT (CASE p.PrimaryLocaleName WHEN 'en-GB' THEN p.ContentText ELSE t.ContentText END)
FROM shortstringresource p
LEFT OUTER JOIN resourcetranslation t ON t.ShortStringResourceID = p.ID
WHERE p.ContentText = "Question 1 English Text"
AND (p.PrimaryLocaleName = 'en-GB' OR t.LocaleName = 'en-GB')
LIMIT 1;
或者将以下 2 个查询加入到 1 个查询中:
var qry1 = (from p in I18nObjects.ShortStringResources
where (p.PrimaryLocaleName == "en-GB" && p.ContentText == "my text")
select p.ContentText);
var qry2 = (from t in I18nObjects.ResourceTranslations
where t.LocaleName == "en-GB"
join p in I18nObjects.ShortStringResources on t.ShortStringResourceID equals p.ID
select t.TranslatedText);
最佳答案
您可能需要在选择之前考虑订购。
var results = (from p in ShortStringResources
join t in ResourceTranslations on p.ID equals t.ShortStringResourceID into xy
from x in xy.DefaultIfEmpty()
where p.ContentText == "Question 1 English Text" &&
(p.PrimaryLocaleName == "en-GB" || x.LocaleName == "en-GB")
select new {
newField = p.PrimaryLocaleName == "en-GB"? p.ContentText : x.ContentText
}).ToList().Take(1);
关于c# - 使用 2 个表之间的可选/条件联接进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15229443/