Linq To SQL: Retain list order when using .Contains

Posted by rockinthesixstring on Stack Overflow See other posts from Stack Overflow or by rockinthesixstring
Published on 2010-12-30T23:01:56Z Indexed on 2010/12/30 23:54 UTC
Read the original article Hit count: 177

Filed under:
|
|

I'm using Lucene.net to build a MyListOfIds As List(Of Integer) which I then pass on to my Linq service. I then search the database as follows

Return _EventRepository.Read().Where(Function(e) MyListOfIds.Contains(e.ID)).ToList

Now I know that Lucene is already ordering MyListOfIds based on the weight it gave each term. What sucks is that Linq is losing that order in it's SQL search.

My Question: How can I retain that sort order when building my Lambda expression?

I tried using LINQPad to see how the query is being built, but because I had to declare a variable LINQPad didn't show me the resultant SQL :-(

Here's what I tried in LINQPad

Dim i As New List(Of Integer)
i.Add(1)
i.Add(100)
i.Add(15)
i.Add(3)
i.Add(123)

Dim r = (From e In Events
         Where i.Contains(e.ID)
         Select e)

note: my example is in VB.NET, but I don't mind if responses are in C#

© Stack Overflow or respective owner

Related posts about .NET

Related posts about linq-to-sql