Clever Humans

Logo

Clever Humans do the darnedest things!

Categories

4 April 2008

A Basic use of Max in Linq

by Patrick

It took me an unfortunately long time to figure out the Linq equivalent to the following SQL Statement:

SELECT Max(SomeField)
FROM SomeTable
WHERE SomeOtherField = 1

I tried every combination of putting Max somewhere I could think of. Turns out the trick is to put it *outside* of the from statement. So, the resulting Linq looks like this:

(from p in blah.SomeTable
where p.SomeOtherField == 1
select p.SomeField).Max()

Perhaps that seems obvious to everyone else. But it took me awhile to “think outside the from” so to speak.

tags: programming