Thursday, February 2, 2012


Various times we need to implement the functionality of ISNULL() in LINQ. There is no any direct method which implement this but we can do it very easily by using DefaultEmpty method and ternary operator.

In this example,we will focus on Reg_No which is an auto increment field.To implement auto increment we can just write "Reg_No = data.Registrations.Max(re => re.Reg_id) + 1,"  . But this will give error when table is blank or the field returns null. To solve this we need ISNULL functionality.
So, here is the solution for u --:
Registration reg = new Registration()


Reg_No = data.Registrations.DefaultIfEmpty().Max(re => (re.Reg_id == null ? 0 : re.Reg_id)) + 1,

 Reg_date = TxtDate.Value,

Reg_Prog_id = Convert.ToInt32(ddlprogram.SelectedValue),

Reg_Course_id = Convert.ToInt32(ddlCourse.SelectedValue),


Still face problem then ask me.Till Enjoy Coding.

No comments:

Post a Comment