JohanLarsson
JohanLarsson

Reputation: 485

Dynamic LINQ select clause

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

Answers (1)

macwier
macwier

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

Related Questions