linq join,group by来获取子表的计数

时间:2014-04-08 07:24:39

标签: c# linq count group-by left-join

在我开始工作之后,我似乎已经陷入了计数位。 我在下面尝试的是获取基于以下联接打印的文档数。 根据' guardiandocs需要计算的代码是什么?

var guardianEntityType = new {EntityTypeFK = "GUARDIAN"};

return (from d in dbContext.GuardianDocsRequireds
        join p in dbContext.DocumentPrintingLogs on
        new { docTypeFK = d.DocTypeFK, entityFK = d.GuardianFK } equals
        new { docTypeFK = p.DocTypeFK, entityFK = p.EntityFK }
        where d.GuardianFK == entityPK && p.ItemGroupFK == itemGroupID && p.EntityTypeFK == "GUARDIAN"
        group d by new
        {
            d.GuardianFK,
            d.DocTypeFK,
            d.DocumentType.DocTypeDescription,
            d.RequiredStatus
        }
            into res
            select new DocumentsRequired
            {
                EntityPK = res.Key.GuardianFK,
                EntityType = entityType,
                DocTypeFK = res.Key.DocTypeFK,
                DocTypeDescription = res.Key.DocTypeDescription,
                RequiredStatus = res.Key.RequiredStatus,
                PrintCount = ???
            }
        ).ToList();

如果有帮助的话,我已经编写了sql来完全按照我的要求生成如下:

    SELECT gdr.DocRequiredID,gdr.RequiredDate,gdr.GuardianFK,gdr.DocTypeFK,gdr.RequiredStatus,
COUNT(dpl.DocPrintedID) AS documentsPrinted
        FROM dbo.GuardianDocsRequired gdr
        LEFT OUTER JOIN dbo.DocumentPrintingLog dpl ON gdr.DocTypeFK = dpl.DocTypeFK
            AND gdr.GuardianFK = dpl.EntityFK
            AND dpl.EntityTypeFK = 'GUARDIAN'
        WHERE gdr.GuardianFK = @entityPK
        GROUP BY gdr.DocRequiredID,gdr.RequiredDate,gdr.GuardianFK,gdr.DocTypeFK,gdr.RequiredStatus

1 个答案:

答案 0 :(得分:0)

你是说这样的吗?

var guardiandocsrequired =  (from d in dbContext.GuardianDocsRequireds
                            join p in dbContext.DocumentPrintingLogs on
                            new { docTypeFK = d.DocTypeFK, entityFK = d.GuardianFK } equals
                            new { docTypeFK = p.DocTypeFK, entityFK = p.EntityFK }
                            where d.GuardianFK == entityPK && p.ItemGroupFK == itemGroupID && p.EntityTypeFK == "GUARDIAN"
                            group d by new
                            {
                                d.GuardianFK,
                                d.DocTypeFK,
                                d.DocumentType.DocTypeDescription,
                                d.RequiredStatus
                            }
                                into res
                                select new DocumentsRequired
                                {
                                    EntityPK = res.Key.GuardianFK,
                                    EntityType = entityType,
                                    DocTypeFK = res.Key.DocTypeFK,
                                    DocTypeDescription = res.Key.DocTypeDescription,
                                    RequiredStatus = res.Key.RequiredStatus,
                                    PrintCount = ???
                                }
                            ).ToList();

int cnt = guardiandocsrequired.Count;

return guardiandocsrequired;