How to Use Linq to Select Object With Minimum or Maximum Property Value

How to use LINQ to select object with minimum or maximum property value

People.Aggregate((curMin, x) => (curMin == null || (x.DateOfBirth ?? DateTime.MaxValue) <
curMin.DateOfBirth ? x : curMin))

Min Max values using a linq query and select box

What about:

var MaxEngineSize;
var MinEngineSize;

IEnumerable<Car> filteredCars = CarID.Where(x => x.EngineSize >= MinEngineSize && x.EngineSize <= MaxEngineSize)

If you want to use LINQ like a pro:

var MaxEngineSize;
var MinEngineSize;
bool EngineFilter(Car c) => c.EngineSize >= MinEngineSize && c.EngineSize <= MaxEngineSize;

var Manifacturer = "Toyota";
bool ManifacturerFiler(Car c) => c.Manifacturer == Manifacturer;


IEnumerable<Car> filteredCars = CarID.Where(EngineFilter)
.Where(ManifacturerFiler);

In this way you can stack checks to create a filter for the cars you're displaying.

How to perform .Max() on a property of all objects in a collection and return the object with maximum value

We have an extension method to do exactly this in MoreLINQ. You can look at the implementation there, but basically it's a case of iterating through the data, remembering the maximum element we've seen so far and the maximum value it produced under the projection.

In your case you'd do something like:

var item = items.MaxBy(x => x.Height);

This is better (IMO) than any of the solutions presented here other than Mehrdad's second solution (which is basically the same as MaxBy):

  • It's O(n) unlike the previous accepted answer which finds the maximum value on every iteration (making it O(n^2))
  • The ordering solution is O(n log n)
  • Taking the Max value and then finding the first element with that value is O(n), but iterates over the sequence twice. Where possible, you should use LINQ in a single-pass fashion.
  • It's a lot simpler to read and understand than the aggregate version, and only evaluates the projection once per element

C# Using LINQ to filter each Object with Max Value in List of Objects

You can achieve the desired result at once with a simple GroupBy and subsequent ordering within each group:

var highestByTitle = list
.GroupBy(t => t.title)
.Select(g => g.OrderByDescending(t => t.val).First())
.ToList();

Demo.

Select records with max property value per group

You can do that with the following Linq.

var results = data.GroupBy(r = r.GroupValue)
.OrderByDescending(g => g.Key)
.FirstOrDefault()
?.GroupBy(r => r.GroupName)
.Select(g => g.OrderByDescending(r => r.MemberValue).First());

First you have to group on the GroupValue then order the groups in descending order by the Key (which is the GroupValue) and take the first one. Now you have all the rows with the max GroupValue. Then you group those on the GroupName and from those groups order the MemberValue in descending order and take the First row to get the row in each GroupName group with the max MemberValue. Also I'm using the C# 6 null conditional operator ?. after FirstOrDefault in case data is empty. If you're not using C# 6 then you'll need to handle that case up front and you can just use First instead.

How can I use LINQ to get the instance with the highest value of a property in a child list?

You can select the count of each item in SecondClass and use Max to find the maximum value. Then you select this value of each item in FirstClass and use Max again:

int highestCount = input.Select(x => x.SecondClass.Select(y => y.Count).Max()).Max();

If you want to find the item with the highest count, you can replace the first Select by OrderByDescending and the second Max by FirstOrDefault:

var itemWithHighestCount = input.OrderByDescending(x => x.SecondClass.Select(y => y.Count).Max()).FirstOrDefault();

Online demo: https://dotnetfiddle.net/0rWARC



Related Topics



Leave a reply



Submit