Skip to content

Instantly share code, notes, and snippets.

@masaru-b-cl
Created November 4, 2016 02:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masaru-b-cl/d6eddbac5b572bf3b914d595c134eec9 to your computer and use it in GitHub Desktop.
Save masaru-b-cl/d6eddbac5b572bf3b914d595c134eec9 to your computer and use it in GitHub Desktop.
class Program
{
static void Main()
{
var source = new [] {
new { Category = "A", Amount = 1000m },
new { Category = "B", Amount = 1000m },
new { Category = "B", Amount = 1000m },
new { Category = "C", Amount = 1000m },
new { Category = "C", Amount = 1000m },
new { Category = "C", Amount = 1000m },
};
var results = source
.GroupBy(x => x.Category)
.Select(g => new { Category = g.Key, Amount = g.Sum(x => x.Amount)});
foreach (var x in results)
{
Console.WriteLine("Category:{0}, Amount:{1}", x.Category, x.Amount);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment