Skip to content

Instantly share code, notes, and snippets.

@posaunehm
Created April 14, 2013 15:41
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 posaunehm/5383132 to your computer and use it in GitHub Desktop.
Save posaunehm/5383132 to your computer and use it in GitHub Desktop.
Get Max value among grouped objects
using System;
using System.Linq;
namespace EnumurateAdjacent
{
class Program
{
static void Main(string[] args)
{
var rand = new Random();
var list =
Enumerable.Range(0, 100).Select(i => new SomeClass {ID = rand.Next(10), Value = rand.NextDouble()});
var query = list.GroupBy(someClass => someClass.ID).Select(
classes => classes.Aggregate(SomeClass.Min,
(ele, acc) =>
ele.Value > acc.Value ? ele : acc)).ToList();
}
}
internal class SomeClass
{
public double Value { get; set; }
public int ID { get; set; }
public static SomeClass Min
{
get { return new SomeClass {Value = double.MinValue}; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment