Note that there are some explanatory texts on larger screens.

plurals
  1. POSelect Contains With CSV But Keep Sort Order
    primarykey
    data
    text
    <p>I have the scenario where I have a <code>IList&lt;Guid&gt;</code> in a variable called csv which are also in a specific order that I need to keep. I am then doing a select contains like so I can get back all my topics based in the list of guids I have.</p> <p>The guids are from a lucene search which are ordered by the original score from each LuceneResult. Which is why I need to keep them in this order.</p> <pre><code> var results = _context.Topic .Where(x =&gt; csv.Contains(x.Id)); </code></pre> <p>However. I lose the order the guids came in as soon as I do this. Any idea how I can do this but keep the same order I hand the list of guids to the context and get the topics back in the same order based on the topid.Id?</p> <p>I have tried the following as mentioned below, by doing a join but they still come out in the same order? Please note that I am paging these results too.</p> <pre><code> var results = _context.Topic .Join(csv, topic =&gt; topic.Id, guidFromCsv =&gt; guidFromCsv, (topic, guidFromCsv) =&gt; new { topic, guidFromCsv } ) .Where(x =&gt; x.guidFromCsv == x.topic.Id) .Skip((pageIndex - 1)*pageSize) .Take(pageSize) .Select(x=&gt; x.topic); </code></pre> <h2>** UPDATE **</h2> <p>So I have moved away from just using and guid and am attempting to pass in my lucene model which has the score property that I want to order by. Here is what I have</p> <pre><code> public PagedList&lt;Topic&gt; GetTopicsByLuceneResult(int pageIndex, int pageSize, int amountToTake, List&lt;LuceneSearchModel&gt; luceneResults) { var results = _context.Topic .Join(luceneResults, topic =&gt; topic.Id, luceneResult =&gt; luceneResult.Id, (topic, luceneResult) =&gt; new { topic, luceneResult } ) .Where(x =&gt; x.luceneResult.Id == x.topic.Id) .OrderByDescending(x =&gt; x.luceneResult.Score) .Skip((pageIndex - 1) * pageSize) .Take(pageSize) .Select(x =&gt; x.topic); var topicResults = results.ToList(); // Return a paged list return new PagedList&lt;Topic&gt;(topicResults, pageIndex, pageSize, topicResults.Count); } </code></pre> <p>However I am now getting the following error? Is what I am doing possible? </p> <p><strong>Unable to create a constant value of type 'LuceneSearchModel'. Only primitive types or enumeration types are supported in this context.</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload