Reputation: 2251
I've been struggling on this for a whole day now.
So my class looks like follows:
public class SupplierSummaryReport {
public string SupplierName { get; set; }
public decimal TurnOverValues { get; set; }
}
The LINQ so far looks like this:
var data = (from i in _ctx.Invoices
where i.Turnover == true
where last5Years.Contains(i.InvoiceDate.Year)
select new {
Year = i.InvoiceDate.Year,
AccountName = i.AccountName,
Value = i.NetAmount_Home ?? 0
});
var y = data.GroupBy(r => new { r.Year, r.AccountName })
.Select(g => new APData.Audit.Models.ReportModels.SupplierSummaryReport {
SupplierName = g.Key.AccountName,
TurnOverValues = new List<decimal>{
g.Sum(r => r.Value)
}
});
This gives me data which looks like this:
{2014, "Test Supplier", 1}, {2012, "Test Supplier", 2}, {2015, "Test Supplier two", 5}, {2011, "Test Supplier two", 9}
I need to use .SelectMany
to select the SupplierName
and the TurnOverValues
. However I need to put these values into a separate list of classes like follows:
public class SupplierSummaryData {
public string SupplierName { get; set; }
public List<decimal> Values { get; set; }
}
I've been looking to use LINQ but I haven't been able to do it thus far. I understand that this issue is very specific but I have no where else to turn, if you need more information, please leave a comment.
Expected output:
{"Test Supplier", {2,5}}, {"Test Supplier two", {5,6}}
Upvotes: 1
Views: 2492
Reputation: 27871
You need to group by the AccountName
only and then for each group select the Supplier Name and the list of values like this:
var result =
data.GroupBy(r => r.AccountName)
.Select(g => new APData.Audit.Models.ReportModels.SupplierSummaryReport
{
SupplierName = g.Key,
TurnOverValues = g.Select(r => r.Value).ToList()
});
Upvotes: 5