Using Case statement in LINQ to SQL
Posted on 8/27/2010 6:15:50 AM
in #Asp.Net 3.X
Working with LINQ I realized that I had to use the simple case statement in my SQL query. There is no special keyword for this. To create a case statement like structure you will have to do it in the select section of the query./o:p>/SPAN>/P>
Below is an example of the usage of the case statement in LINQ./o:p>/SPAN>/P>
var t = from n in idc.categories /SPAN>select new /SPAN>{ /SPAN> /SPAN>catName = /SPAN>(n.id==1 ? “Cat1” : /SPAN>n.id==2 ? “Cat2” : /SPAN>n.id==3 ? “Cat3” : “Unknown Category” /SPAN>)/o:p>/SPAN>/P>
/SPAN>};/o:p>/SPAN>/P>
Here in the above code we are using multiple cases for value 1, 2 and 3 and the default value is given at the last./o:p>/SPAN>/P>
Vikram/o:p>/SPAN>/P>
|