What is the equivalent of "CASE WHEN THEN" (T-SQL) with Entity Framework?

Viewed 23361

I have a Transact-SQl request that I use a lot and I want to get the equivalent with Entity Framework. But I don't know how to make a "CASE WHEN" statement with EF. Here is a simplified code of my request :

SELECT Code,
SUM(CASE WHEN Month=1 THEN Days Else 0 END) AS Jan,
FROM MyTable 
GROUP BY Code

Can you tell me if it's possible and how to do this with EF ?

4 Answers

If you're using method syntax, this is what I use and find readable

Status = 
    s.Status == "Active" ? "1 Active" :
    s.Status == "Storage" ? "2 Storage" :
    s.Status == "Deactivated" ? "3 Deactivated" :
    "9 Unknown",

where the last value, is the else or catch all

Related