Created
December 28, 2011 18:16
-
-
Save smoak/1528973 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CountItem<T> | |
{ | |
public int Count { get; set; } | |
public T Value { get; set; } | |
} | |
var states = new List<string> | |
{ | |
"WA", | |
"WA", | |
"WA", | |
"CA", | |
"OR", | |
"WI", | |
"OR", | |
"ID", | |
"NM", | |
"LA", | |
"WA" | |
}; | |
var distinctStates = from s in states | |
group s by s into grp | |
select new CountItem<string> { Value = grp.Key, Count = grp.Count() }; | |
foreach(var row in distinctStates.OrderBy(x => x.Value)) | |
{ | |
Console.WriteLine("{0}: {1}", row.Value, row.Count); | |
} | |
/* Outputs: | |
CA: 1 | |
ID: 1 | |
LA: 1 | |
NM: 1 | |
OR: 2 | |
WA: 4 | |
WI: 1 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment