Reputation: 485
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?
Upvotes: 2
Views: 2039
Reputation: 1083
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.
Upvotes: 1