我试图实现一个有点棘手的Linq查询。
所有字段都是必填字段。
我想要检索:
答案 0 :(得分:0)
首先,您需要使用LEFT JOIN
和tableD
tableB
tableC
。
var augmentedD =
from d in tableD
join b in tableB on d.IdB equals b.IdB into bs
join c in tableC on d.IdC equals c.IdC into cs
from b in bs.DefaultIfEmpty()
from c in cs.DefaultIfEmpty()
let idAA = b != null ? b.IdA : c != null ? c.IdA : (int?) null
where idAA != null
let idA = idAA.Value
select new
{
idA,
d
};
然后,GroupJoin
tableA
到该结果。
var nestedA = from a in tableA
join d in augmentedD on a.IdA equals d.idA into g
select
new
{
A = a,
D = g.OrderBy(x => x.d.NameD)
.Take(10)
.Select(x => x.d)
.ToList()
};
如果您想要添加其他谓词,请在Where
之后添加tableD
语句
var augmentedD =
from d in tableD.Where(predicate)
join b in tableB on d.IdB equals b.IdB into bs
join c in tableC on d.IdC equals c.IdC into cs
from b in bs.DefaultIfEmpty()
from c in cs.DefaultIfEmpty()
let idAA = b != null ? b.IdA : c != null ? c.IdA : (int?) null
where idAA != null
let idA = idAA.Value
select new
{
idA,
d
};