Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write SQL IsNull() in Linq? [duplicate]

Possible Duplicate:
Equivalent of SQL ISNULL in LINQ?

I am recently migrated from ADO.Net to Entity Framework

I have problem in executing this query in Linq

select IsNull(MAX(InvoiceNo),0) from Sales

I have written this query in LINQ except isNull()

        var q = (from p in dbContext.Sales
                 select p.InvoiceNo).Max();

But i dont know how to use IsNull() in this Linq

When i execute this Linq Query I am getting this exception.

'The invocation of the constructor on type 'JIMS.ViewModel.Transaction.SalesViewModel' that matches the specified binding constraints threw an exception.' Line number '8' and line position '6'.

I am using WPF MVVM FrameWork with Entity FrameWork as DAL

like image 235
Kishore Kumar Avatar asked Dec 16 '22 02:12

Kishore Kumar


2 Answers

var InvoiceNo = dbContext.Sales.Max(x => (int?)x.InvoiceNo) ?? 0;
like image 101
Magnus Avatar answered Jan 01 '23 21:01

Magnus


Try this:

var q = (from p in dbContext.Sales
                 select (int?)p.InvoiceNo).Max();
like image 22
Kyberias Avatar answered Jan 01 '23 22:01

Kyberias