c# - Counting occurrences of the first x elements of sublist in all sublists of the jagged list without grouping -
i'm having jagged list.
var jaggedlist = new list<list<string>>() { cc kk ww } { cc kk aa } { cc oo ll } { cc jj oo } { ww oo kk } { ww oo gg } { ww gg kk } { kk ll oo } { ll kk nn } { mm nn oo } { mm nn jj }
i'm having linq command want grouping.
var dic = jaggedlist.groupby(lst => string.join(" ", lst.take(2))) .todictionary(el => el.key, el => el.count());
now want command edited not group.
i not sure want, please check it:
p.s. dic variable not dictionary, because if want same count jaggedlist, there key dublicates inadmissible dictionary collection.
var n = 2; var jaggedlist = new list<list<string>>(){ new list<string>{ "cc", "kk", "ww" }, new list<string>{ "cc", "kk", "aa" }, new list<string>{ "cc", "oo", "ll" }, new list<string>{ "cc", "jj", "oo" }, new list<string>{ "ww", "oo", "kk" }, new list<string>{ "ww", "oo", "gg" }, new list<string>{ "ww", "gg", "kk" }, new list<string>{ "kk", "ll", "oo" }, new list<string>{ "ll", "kk", "nn" }, new list<string>{ "mm", "nn", "oo" }, new list<string>{ "mm", "nn", "jj" } }; func<ienumerable<string>, int, string> getkey = (col, num) => col.take(num).aggregate((a, b) => + " " + b); var dic = jaggedlist.select(x => { var key = getkey(x, n); return new { key, count = jaggedlist.where(y => getkey(y, n) == key).count() }; }).tolist();
Comments
Post a Comment