Dynamic LINQ select clause

dynamic-linq if-statement linq linq-to-sql select

Question

I'm using Dynamic LINQ to SQL and also to objects in my application. I have googled around for a solution similar to a sql case, such as the one below.

SELECT case when [var] = 'Foo' then 'A' when [var] = 'Bar' then 'B' end
FROM [db].[dbo].[Table]
WHERE 1=1

If have tried with IF(condition,true,false) like the below but without success.

var query = db.Table.AsQueryable().Where("1==1")
.Select("new(IF(var==\"Foo\",\"A\",\"B\") as TestVar)");

Any pointers?

1
2
7/28/2014 11:50:14 AM

Accepted Answer

I'm not sure, but i think you should try the following:

var query = db.Table.AsQueryable().Where("1==1")
.Select("new(var==\"Foo\" ? \"A\" : var==\"Bar\" ? \"B\" : null as TestVar)");

I don't know what are the rules for escaping string in dynamic LINQ, but it should point you in the right direction.

1
7/28/2014 12:25:34 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow