0
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);
4

2 に答える 2

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);
于 2013-03-05T16:56:07.347 に答える
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( );

(しかし、多分私はあなたの質問を誤解しました)

于 2013-03-05T17:14:19.247 に答える