If I want to get the min type of a collection, applying some criteria of the class values for a complex class with a lot of variables and fields, that cannot be done directly.
The closest attempt is:
public static TSource Min<TSource>(this IEnumerable<TSource> source);
For a simple class its clear:
public static void MaxEx3()
{
Pet[] pets = { new Pet { Name="Barley", Age=8 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=1 } };
Pet max = pets.Max();
Console.WriteLine(
"The 'maximum' animal is {0}.",
max.Name);
}
/*
This code produces the following output:
The 'maximum' animal is Barley.
*/
But for a type with many fields there is no implementation such as: "return this specific type where the value of a determined fiel is minimal". For example from a Vector3
collection the one with the minimum z
.
Closest workaround would be returning a collection of your type like this (for a Vector3
type):
OrderBy( vector => vector.z).Take(1).ToList();
I found this quite curious, as I find the source type return with a criteria in determined class field type or with any other predicate appliying logic the check of the class values themselves quite handy. Is there any explanation for this?