Frequency Count In LINQ
-
Good Afternoon. I have a list called colours I have the following LINQ statement that returns that list var foo = from s in colours select s ; What I need to do is also select the frequency, so say the list contained red,blue,red,yellow,yellow,green,green,yellow I would get returned red 2 blue 1 yellow 3 green 2 Is this possible using LINQ? any advise, links keywords most welcome Regards Mark
-
Good Afternoon. I have a list called colours I have the following LINQ statement that returns that list var foo = from s in colours select s ; What I need to do is also select the frequency, so say the list contained red,blue,red,yellow,yellow,green,green,yellow I would get returned red 2 blue 1 yellow 3 green 2 Is this possible using LINQ? any advise, links keywords most welcome Regards Mark
-
I have now got this, which works, but i need the grouping to be case insensitive var foo = from s in colours group colours by s into ss select new {colour= ss.Key,score=ss.Count()} any help most welcome
Assuming that
s
is type string, you should be able to saygroup colours by s.ToLower()
-
Assuming that
s
is type string, you should be able to saygroup colours by s.ToLower()
-
Yes s is a string, I have tried that, but it means that my results are in lower case :( Thanks
This gives you the first element in the group as the key which I think is what you want. If not, you can change the projection lambda or use another overload of the method.
var foo = colors.GroupBy(color => color,
(key, elements) => new { Color = key, Count = elements.Count() }, // projection
new CaseInsensitiveEqualityComparer());class CaseInsensitiveEqualityComparer : IEqualityComparer<string>
{
#region IEqualityComparer<string> Memberspublic bool Equals(string x, string y) { return x.ToLower() == y.ToLower(); } public int GetHashCode(string obj) { return obj.ToLower().GetHashCode(); } #endregion
}
Eslam Afifi
-
This gives you the first element in the group as the key which I think is what you want. If not, you can change the projection lambda or use another overload of the method.
var foo = colors.GroupBy(color => color,
(key, elements) => new { Color = key, Count = elements.Count() }, // projection
new CaseInsensitiveEqualityComparer());class CaseInsensitiveEqualityComparer : IEqualityComparer<string>
{
#region IEqualityComparer<string> Memberspublic bool Equals(string x, string y) { return x.ToLower() == y.ToLower(); } public int GetHashCode(string obj) { return obj.ToLower().GetHashCode(); } #endregion
}
Eslam Afifi
You don't have to write your own CaseInsensitiveEqualityComparer, there's
StringComparer.CurrentCultureIgnoreCase
. -
You don't have to write your own CaseInsensitiveEqualityComparer, there's
StringComparer.CurrentCultureIgnoreCase
.I remembered such class exists. When I was answering the question I thought it was System.Collections.CaseInsensitiveComparer but I checked and it isn't an IEqualityComparer. Gideon Engelberth suggested ToLower so I decided to go with it at least to illustrate the IEqualityComparer interface. Thanks for reminding me where to find it :) I'll try to never forget it again, it is an IComparer too :cool:
Eslam Afifi