使用StackOverflow和another 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
并填充“总计”列和其他计算或汇总的列?