Get grouped comma separated values with linq
var dCounts =
(from i in dic
group i by i.Value into g
select new { g.Key, count = g.Count(), Items = string.Join(",", g.Select(kvp => kvp.Key)) });
Use string.Join(",", {array})
, passing in your array of keys.
You can use:
var dCounts =
from i in dic
group i by i.Value into g
select new { g.Key, Count = g.Count(), Values = g };
The result created by grouping (value g
) has a property Key
that gives you the key, but it also implements IEnumerable<T>
that allows you to access individual values in the group. If you return just g
then you can iterate over all values using foreach
or process them using LINQ.
Here is a simple dump function to demonstrate this:
foreach(var el in dCounts) {
Console.Write(" - {0}, count: {1}, values:", el.Key, el.Count);
foreach(var item in el.Values) Console.Write("{0}, ", item);
|