Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8873395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:27:15+00:00 2026-06-14T18:27:15+00:00

I got a list that contains items. They all got a ‘Sort’ column. The

  • 0

I got a list that contains items. They all got a ‘Sort’ column. The sort column is of type int, and it’s unique.

Scenario:

sort 1; sort 2; sort 3;

If the user moves an item up (for example sort 3) in the list (for example to position 1, which would give the sort value 1), the items that are under the one that just got moved up, have to be shifted down in the list, and the sort number should be applied accordingly. In this case all shifted items sort – 1.

So the end state of the scenario looks like this:

sort 1 was sort 3; sort 3 was sort 2; sort 3 is now sort 1;

How can i do this with LINQ?
It’s not just 3 items. It can be a lot more.

[Edit]

 public ActionResult Up(int id)
 {
    var item = dataContext.item.FirstOrDefault(x => x.item == id);

    return View(dataContext.items);
 }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:27:18+00:00Added an answer on June 14, 2026 at 6:27 pm

    That might not be the easiest code to understand but I’ve tested it and it seems to work as intended.

    Let’s setup some data.

    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;
    

    First, query is split into three parts depending on positions of old and new indexes, so we can handle each case separately.

    var q = 
        oldSort > newSort ? 
            array
                .Where(x => x.Sort >= newSort && x.Sort < oldSort)
                .Select(x => new { Sort = x.Sort + 1, Value = x.Value })
                .Union(array.Where(x => x.Sort < newSort || x.Sort > oldSort))
                .Union(array.Where(x => x.Sort == oldSort)
                            .Select(x => new { Sort = newSort, Value = x.Value }))
                : 
        oldSort < newSort ?         
            array
                .Where(x => x.Sort <= newSort && x.Sort > oldSort)
                .Select(x => new { Sort = x.Sort - 1, Value = x.Value })
                .Union(array.Where(x => x.Sort > newSort || x.Sort < oldSort))
                .Union(array.Where(x => x.Sort == oldSort)
                            .Select(x => new { Sort = newSort, Value = x.Value }))
                :
        array;
    

    Results for moving an item down (oldSort = 1, newSort = 3):

    1 foo2
    2 foo3 
    3 foo1 
    4 foo4 
    

    Results for moving an item up (oldSort = 4, newSort = 2):

    1 foo1 
    2 foo4 
    3 foo2 
    4 foo3 
    

    UPDATE: The query works by splitting a sequence into three parts

    • Item with the old index becomes an item with the new index;
    • Items between the old and new indexes are shifted either up or down;
    • The rest keeps their indexes.

    The result is the union of the parts.

    UPDATE 2: The query works for any number of items and the absence of loops is intentional.

    UPDATE 3: Here’s one way to make the query work with LINQ-to-Entities.

    using (var context = new TestDBEntities())
    {
        var array = context.TestTables;
        var q =
            oldSort > newSort ?
                array
                    .Where(x => x.Sort >= newSort && x.Sort < oldSort)
                    .Select(x => new { Sort = x.Sort + 1, Value = x.Value })
                    .Union(array.Where(x => x.Sort < newSort || x.Sort > oldSort)
                                .Select(x => new { Sort = x.Sort, Value = x.Value }))
                    .Union(array.Where(x => x.Sort == oldSort)
                                .Select(x => new { Sort = newSort, Value = x.Value }))
                    :
            oldSort < newSort ?
                array
                    .Where(x => x.Sort <= newSort && x.Sort > oldSort)
                    .Select(x => new { Sort = x.Sort - 1, Value = x.Value })
                    .Union(array.Where(x => x.Sort > newSort || x.Sort < oldSort)
                                .Select(x => new { Sort = x.Sort, Value = x.Value }))
                    .Union(array.Where(x => x.Sort == oldSort)
                                .Select(x => new { Sort = newSort, Value = x.Value }))
                    :
            array.Select(x => new { Sort = x.Sort, Value = x.Value });
    }
    

    The difference is that the types are now explicitly compatible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a list that contains al the status items of each order. The
I've got a class that contains (among many other things) a list of items
I've got a list of System.Drawing.RectangleF objects that all overlap the same RectangleF object.
I've got an array of values that i'm using to create list items that
I got an action List //[HttpGet] (will come back to that!) public ViewResult List(int
I've got a routine that grabs a list of all images in a directory,
I've got a List<string> that contains duplicates and I need to find the indexes
I've got a database with one field that contains a comma delimited list that
I've got a form that contains a variable length list of textboxes, rendered using
I've got an arrayController that contains a list of recent search terms. When I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.