LINQ - ISNULL in Max Val - Asked By Shan on 02-May-11 06:13 AM

HI,

 If table is empty I wanted to fetch 0 as we do (isnull,0) in usaul query
 

var RepWidgetVal = (from o in context.ReportWidgets

select Convert.ToInt32(o.WidgetID)).Max();

 

Jitendra Faye replied to Shan on 02-May-11 06:17 AM
You can use this code-

Int32 maxval;

var RepWidgetVal = (from o in context.ReportWidgets

select Convert.ToInt32(o.WidgetID)).Max();

if(RepWidgetVal ==null)
 {
   maxval=0;
 }
else
{
//write code
}

Use this code and let me know.
Riley K replied to Shan on 02-May-11 06:19 AM
See this example 

var result =
           
from prod in ctx.Products
            let val
= prod.Size == "" ? null : prod.Size
           
where prod.Size == null
           
select new { prod.Size, val };
Reena Jain replied to Shan on 02-May-11 06:38 AM
hi shan,

try this which is working for me

var result =
   from product in c.Products
   let val = product.Size == "" ? 0 : product.Size
   where product.Size == null
   select new { product.Size, val };

hope this will help you