带左连接和倍数键的Linq不起作用

本文关键字:Linq 不起作用 连接 | 更新日期: 2023-09-27 18:23:35

我需要帮助!

当我执行以下LINQ查询时,它可以完美地工作(显示结果)

   using (var db = new MyEntities())
   {
    var result = (from dc in db.ClassDiary
                            where dc.DesTurm == dataForm.DesTurma
                            && dc.Module == dataForm.Module
                            && dc.CodDisc == dataForm.CdDisc
                        orderby dc.NrDiary
                        select new ClassDiaryMod
                        {
                            Id = dc.ID,
                            NrDiary = dc.NrDiary,
                            NrStudant = dc.NrStudant,
                            DesTurma = dc.DesTurma,
                            CdDisc = dc.CodDisc,
                            CdTeac = dc.CodTeac,
                            TotalFoult = (from f in db.Foult
                                           where
                                             f.NrStudant == dc.NrStudant &&
                                             f.Disc == dc.CodDisc
                                           select new FoultMod
                                           {
                                               Foults = f.Foult
                                           }).Sum(x => x.Foults)
                        }).ToList();
  return result;

当我尝试应用带有多个键的左联接时,不会显示结果

    using (var db = new FamespEntities())
    {
      var result = (from dc in db.ClassDiary
                              join fn in db.Foult
                                      on new { dc.NrStudant, dc.CodDisc, dc.DesTurm }
                                  equals new { fn.NrStudant, CodDisc = fn.Disc, DesTurm = fn.Desturm } into fn_join
                              from fn in fn_join.DefaultIfEmpty()
                            where dc.DesTurm == dataForm.DesTurm
                            && dc.Module == dataForm.Module
                            && dc.CodDisc == dataForm.CdDisc
                        orderby dc.NroDiary
                        select new ClassDiaryMod
                        {
                            Id = dc.Id,
                            NrDiary = dc.NroDiary,
                            NrStudant = dc.NrStudant,
                            DesTurm = dc.DesTurm,
                            CdDisc = dc.CodDisc,
                            CdTeac = dc.CodTeac,
                            FoultOfDay = fn.Foult,
                            TotalFoults = (from f in db.Foult
                                           where
                                             f.NrStudent == dc.NrStudant &&
                                             f.Disc == dc.CodDisc
                                           select new FoultMod
                                           {
                                               Foults = f.Foult
                                           }).Sum(x => x.Foults)
                        }).ToList();

喜欢理解为什么第一个代码有效,而第二个代码无效。非常感谢

带左连接和倍数键的Linq不起作用

您的

on new { dc.NrStudant, dc.CodDisc, dc.DesTurm }
equals new { fn.NrStudant, CodDisc = fn.Disc, DesTurm = fn.Desturm } 

不正确,应该是

on new { NrStudant = dc.NrStudant, CodDisc = dc.CodDisc, DesTurm = dc.DesTurm }
equals new { NrStudant = fn.NrStudant, CodDisc = fn.Disc, DesTurm = fn.Desturm } 

因此实地比较可以发挥作用。