I'm looking to convert the following SQL into Linq2SQL.
select isnull(max(Id),0) from tbl
Even though I have Id defined as Int NOT NULL
I wish to be able to have a defult value of 0, even when there are no rows in the table.
The best readable approach I've been able to come up with is
var maxId = dc.tbl.Select(row => row.Id)
.ToArray().Union(Enumerable.Range(0, 1)).Max();
But this approach requires bringing down all the data, a more performant but less readable version is
var maxId = dc.tbl.Select(row => row.Id)
.OrderByDescending(ii => ii).FirstOrDefault();
Is there a better way?