ElementAtMax, ElementAtMin and ElementAtAverage

ElementAtMax, ElementAtMin and ElementAtAverage are used to find an element with an attribute that is the smallest largest or average within a set.

Using a set of students as an example, LINQ built in Min, Max and Average will tell you what is the age of the youngest student or the highest GPA in the set but you will have to use that value as the input of another LINQ statement to find the actual student.

Things get even more complicated when looking at average values, the average GPA might be 3.356 but none of the students have that exact GPA.

ElementAtMax, ElementAtMin and ElementAtAverage do just that in a simple and fast manner, when using the ElementAtAverage extension you can specify if you want an exact match or the nearest match if en exact match is not available.

Few Examples:

Student youngest = Sudents.ElementAtMin(x => x.Age);
Student valedictorian = Sudents.ElementAtMax(x => x.GPA);
Student salutatorian = Sudents.Where(x => x != valedictorian ).ElementAtMax(x => x.GPA);
Student takesItEasy = Sudents.ElementAtAverage(x => x.GPA);



Last edited May 29, 2012 at 1:51 PM by AmirLiberman, version 1

Comments

No comments yet.