LeedsWalker
LeedsWalker

Reputation: 199

C# lambda expression with sub query

I need to convert the following SQL statement to a lambda expression so that my C# application can access the data using entity framework. I am struggling to implement the sub query in a lambda expression. The SQL statement is

SELECT columnName1
FROM tableName
WHERE columnName2 = (SELECT MAX(columnName2) FROM tableName)

In the lambda expression I am familiar with the .Select() and the .First() but can't get the syntax quite right for the .Where() when it needs to reference the sub query.

Upvotes: 0

Views: 179

Answers (1)

gabriel.hayes
gabriel.hayes

Reputation: 2313

Assuming tableName is an IEnumerable<T> where T has members columnName1 and columnName2 and columnName2 is a decimal, int, float, long or double:

tableName
   .Where(row => row.columnName2 == tableName.Max(row => row.columnName2))
   .Select(row => row.columnName1);

Upvotes: 1

Related Questions