在多个上下文中将聚合列添加到Entity Framework Join()

时间:2015-11-11 20:08:18

标签: .net linq entity-framework-5

使用StackOverflowanother site的结果,我可以加入多个上下文。问题是我还想选择一个GroupBy计数的附加列。

reports =
   contacts.Join(records, c => c.ContactId, r => r.RecordId,
       (c, r) => new ReportModel
       {
           Record = r.RecordId,
           Available = r.RecordAvailable,
           Result = c.ContactResult,
           Date = c.ContactDate,
           Account = c.ContactId,
           //Total = r.Count()
       }).ToList();

显然,我不能简单地期望r.Count()将像示例一样工作。我尝试过以下操作,但是在创建包含其他列的完整ReportModel时出现问题。

.GroupBy(r => new {r.RecordId})
.Select(x => new ReportModel {
           Record = r.RecordId,
           Available = r.RecordAvailable,
           Result = c.ContactResult,
           Date = c.ContactDate,
           Account = c.ContactId,
           Total = x.Count(r => r.RecordId)
  });

如何使用现有的ReportModel并填充“总计”列和其他计算或汇总的列?

0 个答案:

没有答案