c# - List<int> filtering using linq -
i have list object contains id values example contains: 1,2,10,1,23,11,1,4,2,2,.. etc need find out how many times "1","2",... etc have occured using linq in c#
kindly help.
that's pretty simple using enumerable.groupby
:
var grouped = list.groupby(x => x); foreach (var group in grouped) { console.writeline("{0} appears {1} times", group.key, group.count()); }
or alternatively:
var query = list.groupby(x => x, (x, g) => new { key = x, count = g.count() })); foreach (var result in query) { console.writeline("{0} appears {1} times", result.key, result.count); }
(the difference in when transform group result, really.)
Comments
Post a Comment