shamim
shamim

Reputation: 6768

How to get the max row number from a table by using linq

SELECT ISNULL(MAX(ColumnName) + 1, 1) AS ColumnName 
FROM TableName

How to write this query in LINQ.Is there any way to convert sql to linq . i want a converter.

Above query works well but i want the same output on linq .How to ?

I know how to select max

  var products = ((from p in db.TableName
                            select p.ColumnName).Max());

Upvotes: 0

Views: 517

Answers (1)

Julien Lebosquain
Julien Lebosquain

Reputation: 41223

This should do it:

return (myContext.MyTable.Max(t => (int?) t.MyColumn) ?? 0) + 1

Upvotes: 2

Related Questions