C# Linq到Sql本地化查询
给定下表,我想返回给定区域性的本地化文本或默认区域性的文本,其中给定区域性不存在行 下面的数据也是如此 资源C# Linq到Sql本地化查询,c#,linq-to-sql,localization,left-join,outer-join,C#,Linq To Sql,Localization,Left Join,Outer Join,给定下表,我想返回给定区域性的本地化文本或默认区域性的文本,其中给定区域性不存在行 下面的数据也是如此 资源 ID Name 1 Donkey 2 Elephant ID CultureID ResID LocaleText 1 1 1 Donkey 2 1 2 Elephant 3 2 1 baudet LocaleStrings ID Name 1 Donkey 2
ID Name
1 Donkey
2 Elephant
ID CultureID ResID LocaleText
1 1 1 Donkey
2 1 2 Elephant
3 2 1 baudet
LocaleStrings
ID Name
1 Donkey
2 Elephant
ID CultureID ResID LocaleText
1 1 1 Donkey
2 1 2 Elephant
3 2 1 baudet
我希望能够为法国文化返回以下内容
baudet
elephant
我尝试了各种基于左连接示例的查询,但我被卡住了
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;
我相信有更好的方法,但这似乎有效:
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
};
实际上,您需要进行交叉连接,以将所有文化与所有资源相匹配。这里的左连接将法语与一个资源(baudet)匹配,因此从技术上讲,连接不是空的。