【问题标题】:Linq query with left join having multiple tables具有多个表的左连接的 Linq 查询
【发布时间】:2012-03-26 16:23:41
【问题描述】:

这是我的 linq 查询

    from row in (
    from c in db.TabC
    from cpd in db.TabPD
    from slm in db.TabSLM
    where cpd.SLid == slm.SLid
    where c.Id == cpd.CID
    where c.O_Id == 1
    select new { c, cpd, slm })
group row in row.slm.SLType into g
select new
{
    SLType = g.Key,
    EStat = g.Count(r => r.c.EstatID != null),
    Training = g.Count(r => r.cpd.TrId != null),
    TrainingComplete =
        g.Count(r => r.cpd.TrStat == 44),
    AssessmentComplete =
        g.Count(r => r.cpd.CndAssess == 44)
};

我需要对表 db.TabSLM 进行左连接,并获取与该表中一样多的记录。 TabC 是主表,TabSLM 也是。 TabPD 具有引用 TabC 和 TabSLM 的详细记录。使用此查询,当 TabPD 中没有针对 TabC rowid 或 TabSLM rowid 的匹配项时,我没有返回任何记录。我想要的是返回 TabSLM 中存在的记录数,以防没有匹配项。关于如何修改此查询的任何想法?

下面是三个表的示例数据,其中列出了最少的字段

TabC
ID  O_ID   Name
1       1     ABC
2       1     XYZ
3       1     RST

TabPD
RowID    CID    SLid 
1        2      1
2        1      1
3        3      2
4        ...

TabSLM
SLid   SLType
1      single level 
2      Multi level

通过上面的查询,我可以获得 TabC.O_ID == 1 的记录,但不能获得 TabC.O_ID == 3 的记录,因为 TabC.O_ID 没有任何 ID == 3 的记录。我仍然想要显示 SLType,其他列显示为 0

感谢您的宝贵时间...

【问题讨论】:

    标签: .net linq c#-4.0 entity-framework-4


    【解决方案1】:

    你需要使用DefaultIfEmpty

    var query =
        from row in (
          from c in db.TabC
          join cpd in db.TabPD on c.Id equals cpd.CID
          from slm in db.TabSLM.Where(x => cpd.SLid == x.SLid)
                               .DefaultIfEmpty()
          where c.O_Id == 1
          select new { c, cpd, slm })
    group row by row.slm.SLType into g
    select new
    {
        SLType = g.Key,
        EStat = g.Count(r => r.c.EstatID != null),
        Training = g.Count(r => r.cpd.TrId != null),
        TrainingComplete =
            g.Count(r => r.cpd.TrStat == 44),
        AssessmentComplete =
            g.Count(r => r.cpd.CndAssess == 44)
    };
    

    【讨论】:

    • 另外,“将 row.slm.SLType 中的行分组到 g 中”不起作用。 “将 row.slm.SLType 分组为 g”。
    • @user20358 - 我稍微更新了查询。如果没有看到表 TabC、TabPD、TabSLM,我将无法知道预期的结果
    【解决方案2】:

    只需尝试一次:

    var query =
        from row in (
          from c in db.TabC
          join cpd in db.TabPD on c.Id equals cpd.CID into temp
                              from cpd in temp.DefaultIfEmpty()
          from slm in db.TabSLM.Where(x => cpd.SLid == x.SLid)
                               .DefaultIfEmpty()
          where c.O_Id == 1
          select new { c, cpd, slm })
    group row by row.slm.SLType into g
    select new
    {
        SLType = g.Key,
        EStat = g.Count(r => r.c.EstatID != null),
        Training = g.Count(r => r.cpd.TrId != null),
        TrainingComplete =
            g.Count(r => r.cpd.TrStat == 44),
        AssessmentComplete =
            g.Count(r => r.cpd.CndAssess == 44)
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-05
      • 2018-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多