【发布时间】:2017-01-01 16:49:28
【问题描述】:
我有三张桌子。
表 A
id name des table2 table3
1 xyz TableA_des1 null 1
2 abc TableA_des2 1 2
3 hgd TableA_des2 2 3
表 B
id name des Active
1 xyz TableB_des1 1
2 abc TableB_des2 1
3 hgd TableB_des2 1
表 C
id name des Active
1 xyz TableC_des1 1
2 abc TableC_des2 1
3 hgd TableC_des2 1
LINQ 查询
var res = (from a in TableA
where id = 1
join b in TableB on a.table2 equals b.id into ab
from bdata in ab.DefaultIfEmpty()
where bdata.Active = true
join c in TableC on a.table3 equals c.id into ac
from cdata in ac.DefaultIfEmpty()
where cdata.Active = true
select new { data1 = a.name, data2 = bdata?? string.Empty, data3 = cdata?? string.Empty})
about 查询给出 null。调试时变量res 为空。
【问题讨论】:
-
返回的不是
null,而是一个空集合。 Linq 查询不返回null(除非您从中检索到可能为空的特定记录)
标签: entity-framework linq asp.net-mvc-5