我需要将此SQL转换为Linq查询,但一如既往,我无法弄清楚:(
猜猜我需要深入了解Linq。select butik.preferences.pref,isnull(butik.userpreferences.selected,0) as selected
from butik.preferences
left join butik.userpreferences
on butik.preferences.id = butik.userpreferences.pref AND
butik.userpreferences.userid = 2
感谢 基督教
答案 0 :(得分:1)
LINQ中没有left join
的直接翻译。但是有很多方法可以归档你想要的东西:)
from item in butik.preferences
select new
{
Pref = butik.preferences.pref,
IsSelected =
butik.userprefences.Any(up => up.pref == item.id && up.userid == 2)
? butik.userpreferences.First(up => up.pref == item.id && up.userid == 2).selected
: false;
}
答案 1 :(得分:0)
请尝试使用以下代码段。
var Query = (from c in butik.preferences
join v in butik.userpreferences.Where(v => v.userid == 2).DefaultIfEmpty() on c.id equals v.pref into JoinUserPref
from v in JoinUserPref.DefaultIfEmpty()
select new
{
pref = c.pref,
selected = (v.selected == null ? 0 : v.selected)
});
如果以上代码段无效,请尝试使用以下LINQPad。