2

鉴于下表,我想返回给定文化的本地化文本或给定文化不存在行的默认文化的文本。

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

所以有了以下数据

资源

ID  Name
1   Donkey
2   Elephant

语言环境字符串

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

1 回答 1

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
        };
于 2009-05-22T17:44:44.220 回答