Skip to content

Instantly share code, notes, and snippets.

@smoak
Created December 28, 2011 18:16
Show Gist options
  • Save smoak/1528973 to your computer and use it in GitHub Desktop.
Save smoak/1528973 to your computer and use it in GitHub Desktop.
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