我花了几个小时试图找出如何将从linq投影的数据整形为强类型视图。我的问题是我认为我的问题是我不确定如何使用IEnumberable和IGrouping。
这是linq:
var spec = from a in _entities.Approvals
join b in _entities.ApprovalSpecifications on a.HeaderlID equals b.HeaderlID into g
where a.ApprovalID == id
group a by a.HeaderlID into groupedByHeader
select new
{
Key = groupedByHeader.Key,
groupedByHeader
};
有人可以建议我应该采用的方法吗?我认为这个课程效果最好,但正如我所提到的,我不知道如何使用IGrouping来构建一个类。任何帮助表示赞赏!
答案 0 :(得分:1)
这样的东西?
class StronglyTypedGrouping {
public object Key { get; set; } // I can't infer Key type from the snippet.
public IEnumerable<Approval> Approvals { get; set; }
}
var spec = from a in _entities.Approvals
join b in _entities.ApprovalSpecifications on
a.HeaderlID equals b.HeaderlID into g
where a.ApprovalID == id
group a by a.HeaderlID into groupedByHeader
select new StronglyTypedGrouping {
Key = groupedByHeader.Key,
Approvals = groupedByHeader
};