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);
答案 0 :(得分:1)
在选择之前,您可能需要考虑订购。
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);
答案 1 :(得分:1)
据我所知,您想要为特定文本选择翻译,如果此翻译不存在,则需要原始值。所以左连接将是正确的方法。
我会这样做:
var result = ( from p in I18nObjects.ShortStringresource
where p.PrimaryLocaleName == "en-GB"
where p.ContentText == "my text"
from t in I18nObjects.ResourceTranslations
on p.ID equals t.ShortStringResourceID
into tJoinData
from tJoinRecord in tJoinData.DefaultIfEmpty( )
where tJoinRecord.LocaleName == "en-GB"
select tJoinRecord.TranslatedText ?? p.ContentText ).First( );
(但也许我误解了你的问题)