c# - Conditional Group and Select in LINQ with type conversion -
i know there bunch of questions out there on still cannot work due type conversion (i think).
i have list of custom class , group either months (mt) or quarters (qr) depending on user choice. works fine. however, have request allow grouping q413 , rest of 2013 in months. mt , qr strings. quartpos double , equals 4 if q4 in quarters , rest in months.
here have:
list<myclass> results = classlist .groupby(a => new { a.rg, a.tar, a.mt = ((double.parse(a.qr) < quartpos) ? a.mt : 0), a.qr }) .select(g => new myclass { rg = g.select(a => a.rg).first(), tar = g.select(a => a.tar).first(), yr = g.select(a => a.yr).first(), qr = g.select(a => a.qr).first(), mt = g.select(a => a.mt).first(), pp = g.average(a => double.parse(a.pp)), pi = g.sum(a => double.parse(a.pi)), cp = g.average(a => double.parse(a.cp)), ci = g.sum(a => double.parse(a.ci)), = g.sum(a => double.parse(a.it)), = g.sum(a => double.parse(a.to)), cnt = g.select(a => a.dt).distinct().count(), pdvol = g.sum(a => (double.parse(a.pp) <= 1) ? 0 : (double.parse(a.pi) / double.parse(a.pp))) }) .tolist(); this throws 2 errors:
string int type conversion - since mt string : 0 int
invalid anonymous type member declaration.
i m aware have use same syntax in select make sure quarters selected if grouped quarters , months if grouped mounths also.
definition of class:
public class myclass { public string yr; public string qr; public string mt; public string cw; public string tar; public string rg; public double pp; public double pi; public double cp; public double ci; public double it; public double to; public double pd; public double cd; public double fp; public double fi; public double fd; public int cnt; public double pdmw; public double pdvol; } classlist class variables strings - straight csv import.
my question how can group month (mt) conditionally?
a.mt = ((double.parse(a.qr) < quartpos) ? a.mt : 0) you can't create anonymous types this. take @ syntax in documentation.
either want:
a.mt or
propertyname = ((double.parse(a.qr) < quartpos) ? a.mt : 0) in line.
Comments
Post a Comment