Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to SQL Case WHEN in VB.NET?

How do I do a Case WHEN in Linq to SQL (vb.net please).

In SQL it would be like this:

SELECT 
CASE
  WHEN condition THEN trueresult
  [...n]
[ELSE elseresult]
END

How would I do this in Linq to SQL?

like image 887
RichC Avatar asked Dec 03 '22 08:12

RichC


2 Answers

var data = from d in db.tb select new {

CaseResult = If (d.Col1 = “Case1”, "Case 1 Rised", If (d.Col1 = “Case2”, "Case 2 Rised", "Unknown Case"))

};
like image 76
Karin Avatar answered Dec 23 '22 23:12

Karin


Check This Out

var data = from d in db.tb select new {

 CaseResult = (

 d.Col1 == “Case1” ? "Case 1 Rised" :

 d.Col1 == “Case2” ? "Case 2 Rised" :

 "Unknown Case")

 };

Please Note that [ ? Symbol = then] , [ : Symbol = Or].

like image 28
Amr M. Tareef Avatar answered Dec 23 '22 23:12

Amr M. Tareef