title: Is is possible to sort EF query results in the database using "Array.IndexOf" or other code-derived data?
body: <p>The following code works, while needing to have the result fetched from the database first:</p>
  
  <pre><code>// ID's by priority (arbitrary yet limited number; eg. 1 to 10 values)
  var ids = new [] { 3, 1, 2 };
  
  // Should return &quot;item 3&quot; if it exists, else &quot;item 1&quot;, else &quot;item 2&quot;..
  var firstItemByPriority = context.Items
      .Where(i =&gt; ids.Contains(i.Id))
      .ToList() // materialize DB results
      .OrderBy(i =&gt; Array.IndexOf(ids, i.Id))
      .FirstOrDefault(); // ie. TOP 1 / LIMIT 1 / First (by ordering)
  </code></pre>
  <p>Is there a way to write this in a way where the ordering can occur <em>in</em> the generated database query? (Such can be constructed in several different ways when writing SQL manually.)</p>
  <p>While the example above uses <code>Array.IndexOf</code>, the general goal is to sort by derived data <em>not in the database</em>. Using SQL Server specific extensions and/or third-party extensions are fair play.</p>