NoPyGod
NoPyGod

Reputation: 5067

How to Sort an IQueryable by a list of ids

In mysql I can do this:

SELECT id, name FROM pages WHERE id IN (5, 10, 20) ORDER BY FIELD(id, 5, 10, 20);

What is the corresponding syntax using LINQ on an IQueryable?

Basically I'm searching with lucene.net and I want to use the returned IDs to grab the real database entries, in the specific order of relevance.

Upvotes: 0

Views: 1031

Answers (1)

zerkms
zerkms

Reputation: 254886

var ids = new List<int>() { 5, 10, 20 };

var linqQueryResult = foo.Where(...).OrderBy(i => ids.IndexOf(i.id));

Upvotes: 3

Related Questions