user1223444
user1223444

Reputation:

an SQL query in LINQ

I have a table with these columns: CombinationID, IndexID, Value There is a sql query:

SELECT     CombinationID
FROM         CombinationIndex
where IndexID <> 4
group by CombinationID 
order by sum(case indexid when 1 then -Value else Value end) desc

How could I write this query in c# by linq?

Upvotes: 1

Views: 56

Answers (1)

Justin Niessner
Justin Niessner

Reputation: 245509

I think this should just about do it for you (it's all off the top of my head so there could be slight syntactical errors):

var results = context
              .CombinationIndexes
              .Where(i => i.IndexID != 4)
              .GroupBy(i => i.CombinationID)
              .OrderBy(g => g.Sum(i => i.IndexID == 1 ? -i.Value : i.Value))
              .Select(g => g.Key);

Upvotes: 4

Related Questions