Home > Software design >  Entity Framework CASE statement with ORDER BY
Entity Framework CASE statement with ORDER BY

Time:12-09

Can someone give me a hand in converting following SQL to a linq query?

SELECT TOP 1000 CountryID
,CoutryName
,ISO2
FROM dbo.Countries
ORDER BY CASE WHEN ISO2 IN('GB','LU','CA','ES')
THEN 0 ELSE 1 END, CountryID 

CodePudding user response:

OrderBy by CASE can be expressed by ternary operator ? :

var names = new [] {"GB", "LU", "CA", "ES"};
...
query = query.OrderBy(x => names.Contains(x.ISO2) ? 0 : 1);
  • Related