views:

46

answers:

1
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());
+2  A: 

This should do it:

return (myContext.MyTable.Max(t => (int?) t.MyColumn) ?? 0) + 1
Julien Lebosquain
+1 perfect, more concise than my solution - great answer
marc_s
above syntax show me error "operator ?? can not be applied of operands of type int and int"
shamim
I thought your column was nullable. Try the modified syntax with the `int?` cast.
Julien Lebosquain
thanks.it's really helpful for me thanks again.Now if i want to set Table and column name dynamically in linq how to do that.Suppose,My method contain table name and column name How to use them on the above syntax
shamim