Use LINQ to move item to top of list

Is there a way to move an item of say id=10 as the first item in a list using LINQ?

Item A - id =5
Item B - id = 10
Item C - id =12
Item D - id =1

In this case how can I elegantly move Item C to the top of my List<T> collection?

This is the best I have right now:

var allCountries = repository.GetCountries();
var topitem = allCountries.Single(x => x.id == 592);  
var finalList = new List<Country>();
finalList.Add(topitem);
finalList = finalList.Concat(allCountries.Where(x=> x.id != 592)).ToList();

Solution 1:

What do you want to order by, other than the known top item? If you don't care, you can do this:

var query = allCountries.OrderBy(x => x.id != 592).ToList();

Basically, "false" comes before "true"...

Admittedly I don't know what this does in LINQ to SQL etc. You may need to stop it from doing the ordering in the database:

var query = allCountries.AsEnumerable()
                        .OrderBy(x => x.id != 592)
                        .ToList();

Solution 2:

LINQ is strong in querying collections, creating projections over existing queries or generating new queries based on existing collections. It is not meant as a tool to re-order existing collections inline. For that type of operation it's best to use the type at hande.

Assuming you have a type with a similar definition as below

class Item {
  public int Id { get; set; }
  ..
}

Then try the following

List<Item> list = GetTheList();
var index = list.FindIndex(x => x.Id == 12);
var item = list[index];
list[index] = list[0];
list[0] = item;

Solution 3:

Linq generallyworks on Enumerables, so it doesn't now that the underlying type is a collection. So for moving the item on top of the list I would suggest using something like (if you need to preserve the order)

var idx = myList.FindIndex(x => x.id == 592);
var item = myList[idx];
myList.RemoveAt(idx);
myList.Insert(0, item);

If your function returns only an IEnumerable, you can use the ToList() method to convert it to a List first

If you don't preserve the order you can simply swap the values at position 0 and position idx

Solution 4:

var allCountries = repository.GetCountries();
allCountries.OrderByDescending(o => o.id == 12).ThenBy(o => o.id) 

This will insert the object with id=12 at the top of the list and rotate the rest down, preserving the order.