我需要将其展平为新系列中的一个项目。
输入将是IEnumerable集合。基类如下所示:
public class ConversionsResult
{
public int SiteId { get; set; }
public int TotalLeads { get; set; }
public int TotalCalls { get; set; }
public int TotalEmails { get; set; }
public int UniqueVisits { get; set; }
public int MetricYear { get; set; }
public int MetricMonth { get; set; }
public string DeviceCategory { get; set; }
}
扁平化课程将如下所示:
SiteId, MetricMonth, MetricYear SUM(TotalLeads), SUM(TotalEmails), SUM(UniqueVisits), CONCATENATE(DeviceCategory).
注意:
我可以完成另一条路线,但我想使用LINQ。我试图破解我找到的一些解决方案,但这是一团糟,所以我排除了我试过的噪音。
答案 0 :(得分:10)
您需要按(Enumerable.GroupBy) SiteId
,MetricMonth
和MetricYear
进行分组,然后选择以下结果:
var query = list.GroupBy(r => new { r.SiteId, r.MetricMonth, r.MetricYear })
.Select(grp => new
{
SiteId = grp.Key.SiteId,
MetricMonth = grp.Key.MetricMonth,
MetricYear = grp.Key.MetricYear,
TotalLeads = grp.Sum(r=> r.TotalLeads),
TotalEmails = grp.Sum(r=> r.TotalEmails),
UniqueVisits = grp.Sum(r=> r.UniqueVisits),
DeviceCategory = String.Join(",", grp.Select(r=> r.DeviceCategory)),
});