Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>That might not be the easiest code to understand but I've tested it and it seems to work as intended.</p> <p>Let's setup some data.</p> <pre><code>var array = new [] { new { Sort = 1, Value = "foo1", }, new { Sort = 2, Value = "foo2", }, new { Sort = 3, Value = "foo3", }, new { Sort = 4, Value = "foo4", }, }; var oldSort = 1; var newSort = 3; </code></pre> <p>First, query is split into three parts depending on positions of old and new indexes, so we can handle each case separately.</p> <pre><code>var q = oldSort &gt; newSort ? array .Where(x =&gt; x.Sort &gt;= newSort &amp;&amp; x.Sort &lt; oldSort) .Select(x =&gt; new { Sort = x.Sort + 1, Value = x.Value }) .Union(array.Where(x =&gt; x.Sort &lt; newSort || x.Sort &gt; oldSort)) .Union(array.Where(x =&gt; x.Sort == oldSort) .Select(x =&gt; new { Sort = newSort, Value = x.Value })) : oldSort &lt; newSort ? array .Where(x =&gt; x.Sort &lt;= newSort &amp;&amp; x.Sort &gt; oldSort) .Select(x =&gt; new { Sort = x.Sort - 1, Value = x.Value }) .Union(array.Where(x =&gt; x.Sort &gt; newSort || x.Sort &lt; oldSort)) .Union(array.Where(x =&gt; x.Sort == oldSort) .Select(x =&gt; new { Sort = newSort, Value = x.Value })) : array; </code></pre> <p>Results for moving an item down (<code>oldSort = 1</code>, <code>newSort = 3</code>):</p> <pre><code>1 foo2 2 foo3 3 foo1 4 foo4 </code></pre> <p>Results for moving an item up (<code>oldSort = 4</code>, <code>newSort = 2</code>):</p> <pre><code>1 foo1 2 foo4 3 foo2 4 foo3 </code></pre> <p><strong>UPDATE</strong>: The query works by splitting a sequence into three parts</p> <ul> <li>Item with the old index becomes an item with the new index;</li> <li>Items between the old and new indexes are shifted either up or down;</li> <li>The rest keeps their indexes.</li> </ul> <p>The result is the union of the parts.</p> <p><strong>UPDATE 2</strong>: The query works for any number of items and the absence of loops is intentional.</p> <p><strong>UPDATE 3</strong>: Here's one way to make the query work with LINQ-to-Entities.</p> <pre><code>using (var context = new TestDBEntities()) { var array = context.TestTables; var q = oldSort &gt; newSort ? array .Where(x =&gt; x.Sort &gt;= newSort &amp;&amp; x.Sort &lt; oldSort) .Select(x =&gt; new { Sort = x.Sort + 1, Value = x.Value }) .Union(array.Where(x =&gt; x.Sort &lt; newSort || x.Sort &gt; oldSort) .Select(x =&gt; new { Sort = x.Sort, Value = x.Value })) .Union(array.Where(x =&gt; x.Sort == oldSort) .Select(x =&gt; new { Sort = newSort, Value = x.Value })) : oldSort &lt; newSort ? array .Where(x =&gt; x.Sort &lt;= newSort &amp;&amp; x.Sort &gt; oldSort) .Select(x =&gt; new { Sort = x.Sort - 1, Value = x.Value }) .Union(array.Where(x =&gt; x.Sort &gt; newSort || x.Sort &lt; oldSort) .Select(x =&gt; new { Sort = x.Sort, Value = x.Value })) .Union(array.Where(x =&gt; x.Sort == oldSort) .Select(x =&gt; new { Sort = newSort, Value = x.Value })) : array.Select(x =&gt; new { Sort = x.Sort, Value = x.Value }); } </code></pre> <p>The difference is that the types are now explicitly compatible. </p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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