Select objects with minimum or maximum property value in C#

To find the object with the minimum or maximum value of a specific property in C#, you can use the Min or Max LINQ extension methods along with a lambda expression to select the property value to compare. Here's an example:

class MyClass
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Value { get; set; }
}

List<MyClass> list = new List<MyClass>()
{
    new MyClass() { Id = 1, Name = "John", Value = 10 },
    new MyClass() { Id = 2, Name = "Mary", Value = 5 },
    new MyClass() { Id = 3, Name = "Bob", Value = 20 },
    new MyClass() { Id = 4, Name = "Alice", Value = 15 },
};

MyClass minObject = list.OrderBy(x => x.Value).First();
MyClass maxObject = list.OrderByDescending(x => x.Value).First();

In this example, we have a list of MyClass objects with an Id, Name, and Value property. We use the OrderBy method to sort the items by the Value property and return the first item to get the object with the minimum value. We use the OrderByDescending method to sort the items in descending order and return the first item to get the object with the maximum value. Note that in case of a tie, these methods return the first item with the minimum or maximum value.

Alternatively, you can use the MinBy or MaxBy extension methods from the MoreLINQ library, which return the first item with the minimum or maximum value, respectively, according to a specified property selector. Here's an example:

using MoreLinq;

MyClass minObject = list.MinBy(x => x.Value);
MyClass maxObject = list.MaxBy(x => x.Value);

In this example, we use the MinBy and MaxBy methods from the MoreLINQ library along with a lambda expression to select the Value property to compare. The result is the object with the minimum or maximum value, respectively. Note that you need to install the MoreLINQ package from NuGet to use these methods.

  1. C# LINQ select object with minimum property value:

    MyObject minObject = myList.OrderBy(obj => obj.Property).FirstOrDefault();
    
  2. Select objects with maximum property value in C#:

    MyObject maxObject = myList.OrderByDescending(obj => obj.Property).FirstOrDefault();
    
  3. C# LINQ find object with minimum value of property:

    MyObject minObject = myList.Aggregate((min, obj) => obj.Property < min.Property ? obj : min);
    
  4. Select objects with maximum value of property in C#:

    MyObject maxObject = myList.Aggregate((max, obj) => obj.Property > max.Property ? obj : max);
    
  5. C# LINQ select object with minimum property using Min:

    MyObject minObject = myList.Min(obj => obj.Property);
    
  6. Select objects with maximum property using LINQ in C#:

    MyObject maxObject = myList.Max(obj => obj.Property);
    
  7. C# LINQ select object with minimum property using OrderBy:

    MyObject minObject = myList.OrderBy(obj => obj.Property).FirstOrDefault();
    
  8. Select objects with maximum property value using OrderByDescending in C#:

    MyObject maxObject = myList.OrderByDescending(obj => obj.Property).FirstOrDefault();
    
  9. C# LINQ select objects with minimum property using Aggregate:

    MyObject minObject = myList.Aggregate((min, obj) => obj.Property < min.Property ? obj : min);
    
  10. Select objects with maximum property using Aggregate in C#:

    MyObject maxObject = myList.Aggregate((max, obj) => obj.Property > max.Property ? obj : max);
    
  11. C# LINQ select object with minimum property using Where and Min:

    MyObject minObject = myList.Where(obj => obj.Condition).Min(obj => obj.Property);
    
  12. Select objects with maximum property using Where and Max in C#:

    MyObject maxObject = myList.Where(obj => obj.Condition).Max(obj => obj.Property);
    
  13. C# LINQ select objects with minimum property using MinBy:

    MyObject minObject = myList.MinBy(obj => obj.Property);
    
  14. Select objects with maximum property value using MaxBy in C#:

    MyObject maxObject = myList.MaxBy(obj => obj.Property);
    
  15. C# LINQ select object with minimum property using FirstOrDefault:

    MyObject minObject = myList.OrderBy(obj => obj.Property).FirstOrDefault();
    
  16. Select objects with maximum property using FirstOrDefault in C#:

    MyObject maxObject = myList.OrderByDescending(obj => obj.Property).FirstOrDefault();
    
  17. C# LINQ select object with minimum property using GroupBy and Min:

    MyObject minObject = myList.GroupBy(obj => obj.GroupingProperty).Select(group => group.Min(obj => obj.Property)).FirstOrDefault();
    
  18. Select objects with maximum property using GroupBy and Max in C#:

    MyObject maxObject = myList.GroupBy(obj => obj.GroupingProperty).Select(group => group.Max(obj => obj.Property)).FirstOrDefault();
    
  19. C# LINQ select objects with minimum property using Aggregate and Min:

    MyObject minObject = myList.Aggregate((min, obj) => obj.Property < min.Property ? obj : min);
    
  20. Select objects with maximum property value using Aggregate and Max in C#:

    MyObject maxObject = myList.Aggregate((max, obj) => obj.Property > max.Property ? obj : max);