how to check for null inside LINQ?
-
can any1 tell me how 2 check is null for a column in LINQ? for e.g var x = (from c in db.stock select new {c.stockid}).distinct(); then i gave int c = x.max(a=>a.stockid)+1; it works but i want to chek whether stockid is null.if its null then i will replace it with 0 my original query like this select distinct max(isnull(stockid,0))+1 from stock i want this in LINQ ..kindly help me
T.Balaji
-
can any1 tell me how 2 check is null for a column in LINQ? for e.g var x = (from c in db.stock select new {c.stockid}).distinct(); then i gave int c = x.max(a=>a.stockid)+1; it works but i want to chek whether stockid is null.if its null then i will replace it with 0 my original query like this select distinct max(isnull(stockid,0))+1 from stock i want this in LINQ ..kindly help me
T.Balaji
-
can any1 tell me how 2 check is null for a column in LINQ? for e.g var x = (from c in db.stock select new {c.stockid}).distinct(); then i gave int c = x.max(a=>a.stockid)+1; it works but i want to chek whether stockid is null.if its null then i will replace it with 0 my original query like this select distinct max(isnull(stockid,0))+1 from stock i want this in LINQ ..kindly help me
T.Balaji
The C# equivalent of isnull is the ?? operator. So, you can do the following:
var x = (from a in db.stock
select new { a.stockid }).Distinct();
int c = x.Max(a => a.stockid ?? 0) + 1;Or, if you want to filter null values in the query:
var x = (from a in db.stock
select new { stockid = (a.stockid ?? 0) }).Distinct();
int c = x.Max(a => a.stockid) + 1;