grouping - Linq return unique records with count of duplicates -
i have collection of printed documents:
- orderdetailid,submitteddate,printedcount
- 1,1 jan 2106,1
- 1,1 jan 2106,1
- 2,3 jan 2106,1
- 3,5 jan 2106,1
- 4,6 jan 2106,1
i can unique records with:
private static iqueryable<orderfields> getuniquejobs(iqueryable<orderfields> jobs) { iqueryable<orderfields> uniquejobs = jobs.groupby(x => x.orderdetailid).select(group => group.first());<br/> return uniquejobs.orderby(x => x.submitteddate); }
but field printedcount have number of times each document printed:
- 1,1 jan 2106,2
- 2,3 jan 2106,1
- 3,5 jan 2106,1
- 4,6 jan 2106,1
i grateful help
thanks previous swift answers, did not question correctly. can illustrate want slow ugly code, not work ;-)
private static list<orderfields> getuniquejobs(iqueryable<orderfields> jobs) { guid lastorderdetailid = guid.empty; list<orderfields> uniquejobs = new list<orderfields>(); jobs = jobs.orderby(x => x.orderdetailid); foreach (var job in jobs) { guid orderdetailid = job.orderdetailid; if (orderdetailid != lastorderdetailid) { uniquejobs.add(job); } else { uniquejobs[uniquejobs.count - 1].printed++; } } return uniquejobs; }
can help?
try this
var uniquejobs = jobs.groupby(x => new {x.orderdetailid, x.submitteddate}) .select(group => new { orderdetailid = x.key.orderdetailid, submitteddate = x.key.submitteddate, printedcount = x.sum(i => i.printedcount) // or count() dont know values. }); return uniquejobs.orderby(x => x.submitteddate);
Comments
Post a Comment