c# - How can I use SelectMany on my IQueryable of classes? -
i've been struggling on whole day now.
so class looks follows:
public class suppliersummaryreport { public string suppliername { get; set; } public decimal turnovervalues { get; set; } }
the linq far looks this:
var data = (from in _ctx.invoices i.turnover == true 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 looks this:
{2014, "test supplier", 1}, {2012, "test supplier", 2}, {2015, "test supplier two", 5}, {2011, "test supplier two", 9}
i need use .selectmany
select suppliername
, turnovervalues
. need put these values separate list of classes follows:
public class suppliersummarydata { public string suppliername { get; set; } public list<decimal> values { get; set; } }
i've been looking use linq haven't been able far. understand issue specific have no else turn, if need more information, please leave comment.
expected output:
{"test supplier", {2,5}}, {"test supplier two", {5,6}}
you need group accountname
, each group select supplier name , list of values 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() });
Comments
Post a Comment