使用entityframework与LINQ连接

时间:2017-09-14 08:05:57

标签: linq

我尝试使用linq进行左查询:

public IQueryable<Mutatie> GetMutaties()
        {
            var query = (from m in context.Mutatie
                        join d in context.tblDienstverband on m.fkDienstVerbandID equals d.DienstverbandID
                        join med in context.tblMedewerker on d.fkMedewerkerID equals med.MedewerkerID
                        where med.fkKlantID == this.klantId
                        select m).Include(d => d.fkDienstVerbandID);
            return query;
        }

但是如何让它有一个左查询?

谢谢

我现在就这样:

var query = (from m in context.Mutatie
                         join d in context.tblDienstverband on m.fkDienstVerbandID equals d.DienstverbandID into grp
                         from d in grp.DefaultIfEmpty()

                         join med in context.tblMedewerker on d.fkMedewerkerID equals med.MedewerkerID into grp1
                         from med in grp.DefaultIfEmpty()

                         where med.fkMedewerkerID == this.klantId
                         select new { m, d, med });
            return query;

1 个答案:

答案 0 :(得分:1)

它应该是这样的:

注意:如果没有行匹配,则d和med为null

public IQueryable<Mutatie> GetMutaties()
{
    var query = (from m in context.Mutatie
                join d in context.tblDienstverband on m.fkDienstVerbandID equals d.DienstverbandID into grp
                from d in grp.DefaultIfEmpty()

                join med in context.tblMedewerker on d.fkMedewerkerID equals med.MedewerkerID into grp_med
                from med in grp_med.DefaultIfEmpty()

                where med.fkKlantID == this.klantId
                //select new { m, d, med };
                select m;
    return query;
}