I need to flatten it to one item in a new collection.
The input will be an IEnumerable collection. The base class looks like this:
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; }
}
The flattened class will look like this:
SiteId, MetricMonth, MetricYear SUM(TotalLeads), SUM(TotalEmails), SUM(UniqueVisits), CONCATENATE(DeviceCategory).
NOTES:
I could accomplish this another route but I wanted to use LINQ. I tried hacking a few solutions I found but it was a total mess so I've excluded the noise I tried.
You need to group by (Enumerable.GroupBy) SiteId, MetricMonth and MetricYear and then select results like:
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)),
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With