2009-05-21 66 views
2

鑑於以下表格,我想返回給定文化的本地化文本或默認文化的文本,其中給定文化沒有行。Linq to Sql本地化查詢

diagram http://lh4.ggpht.com/_gjsCWAV_CZc/ShW6hC-eozI/AAAAAAAACbY/mXaBfiZtBY8/s400/diagram.png

與如下因素數據

所以

資源

ID Name 
1 Donkey 
2 Elephant 

LocaleStrings

ID CultureID ResID LocaleText 
1 1   1  Donkey 
2 1   2  Elephant 
3 2   1  baudet 

我會希望能夠返回以下爲法國文化

baudet 
elephant 

我已經試過各種基於查詢各地LEFT JOINS我見過的樣本,但我堅持。

var ct = from r in db.Resources 
       join lt in db.LocaleStrings 
        on r.ID equals lt.ResID into res 
       from x in res.DefaultIfEmpty() 
       select new 
       { 
        CultureID = x.CultureID, 
        LocaleText = x.LocaleText, 
        ResID = x.ResID 
       }; 

     var text = 
      from c in db.Cultures 
      join t in ct 
      on c.ID equals t.CultureID into cults 
      from x in cults.DefaultIfEmpty() 
      select x; 
+0

實際上,你需要做一個交叉連接與所有資源匹配所有文化。這裏的左連接符合法語與其中一個資源(baudet)匹配,因此連接在技術上不是空的。 – samiz 2009-05-22 17:47:52

回答

1

我敢肯定有一個更好的辦法,但是這似乎工作:

var ct = 
     from c in db.Cultures 
     from l in db.LocaleStrings 
     from r in db.Resources 
     where r.ID == l.ResID 
     select new 
     { 
      CultureID = c.ID, 
      LocaleText = l.CultureID == c.ID ? l.LocaleText : r.Name, 
      ResID = r.ID, 
      LSID = l.CultureID == c.ID ? l.ID : 0 
     }; 

    var text = 
     from t in ct 
     where t.LSID != 0 || (t.LSID == 0 && !((from ac2 in ct 
               where ac2.LSID > 0 && ac2.CultureID == t.CultureID 
               select ac2.ResID).Contains(t.ResID))) 
     select new 
     { 
      CultureID = t.CultureID, 
      LocaleText = t.LocaleText, 
      ResID = t.ResID 
     };