LINQ Interview Question #128
How do Min, Max, MinBy, and MaxBy differ?
Advanced LINQ, Practical Problems & Senior Interview Scenarios Senior Advanced
Quick Interview Answer
Min and Max return the minimum or maximum value from a sequence or projection. MinBy and MaxBy return the source element whose selected key is minimum or maximum.
Detailed Explanation
Min and Max return the minimum or maximum value from a sequence or projection. MinBy and MaxBy return the source element whose selected key is minimum or maximum.
MinBy and MaxBy are useful when the entire object is required rather than only the extreme scalar value.
Code Example
decimal highestPrice =
products.Max(p => p.Price);
Product? mostExpensive =
products.MaxBy(p => p.Price);