Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Using Max() to select a single row

Tags:

c#

.net

linq

I'm using LINQ on an IQueryable returned from NHibernate and I need to select the row with the maximum value(s) in a couple of fields.

I've simplified the bit that I'm sticking on. I need to select the one row from my table with the maximum value in one field.

var table = new Table { new Row(id: 1, status: 10), new Row(id: 2, status: 20) }  from u in table group u by 1 into g where u.Status == g.Max(u => u.Status) select u 

This is incorrect but I can't work out the right form.

BTW, what I'm actually trying to achieve is approximately this:

var clientAddress = this.repository.GetAll()     .GroupBy(a => a)     .SelectMany(             g =>             g.Where(                 a =>                 a.Reference == clientReference &&                  a.Status == ClientStatus.Live &&                  a.AddressReference == g.Max(x => x.AddressReference) &&                  a.StartDate == g.Max(x => x.StartDate)))     .SingleOrDefault(); 

I started with the above lambda but I've been using LINQPad to try and work out the syntax for selecting the Max().

UPDATE

Removing the GroupBy was key.

var all = this.repository.GetAll();  var address = all             .Where(                 a =>                 a.Reference == clientReference &&                  a.Status == ClientStatus.Live &&                  a.StartDate == all.Max(x => x.StartDate) &&                 a.AddressReference == all.Max(x => x.AddressReference))             .SingleOrDefault(); 
like image 754
Boggin Avatar asked Feb 02 '12 15:02

Boggin


People also ask

What is Max in LINQ?

Max () function in LINQ is used to return the maximum value from the collection. With the help of Max() function, it is easy to find the maximum value from a given data source using Max () function. In the other case, we have to write the code to get the maximum value from the list of values.

How do I select a single column in LINQ?

var Q1 = (ds. Tables[1]. AsEnumerable() .

What is any () in LINQ?

The Any operator is used to check whether any element in the sequence or collection satisfy the given condition. If one or more element satisfies the given condition, then it will return true. If any element does not satisfy the given condition, then it will return false.

What is select method in LINQ?

The Select() method invokes the provided selector delegate on each element of the source IEnumerable<T> sequence, and returns a new result IEnumerable<U> sequence containing the output of each invocation.


2 Answers

I don't see why you are grouping here.

Try this:

var maxValue = table.Max(x => x.Status) var result = table.First(x => x.Status == maxValue); 

An alternate approach that would iterate table only once would be this:

var result = table.OrderByDescending(x => x.Status).First(); 

This is helpful if table is an IEnumerable<T> that is not present in memory or that is calculated on the fly.

like image 85
Daniel Hilgarth Avatar answered Sep 28 '22 07:09

Daniel Hilgarth


You can also do:

(from u in table orderby u.Status descending select u).Take(1); 
like image 32
KAPIL SHARMA Avatar answered Sep 28 '22 07:09

KAPIL SHARMA