Created
April 14, 2013 15:41
-
-
Save posaunehm/5383132 to your computer and use it in GitHub Desktop.
Get Max value among grouped objects
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
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