First, you can do that directly in the query:
var list = db.Teams.OrderBy(t => t.Name == "Total" ? 1 : 0).ToList();
But if you insist doing it after the list is populated, then you can use the FindIndex
method to find the index of the total item (if any) and them move the item to the end using RemoveAt
and Add
methods:
var list = db.Teams.ToList();
int index = list.FindIndex(t => t.Name == "Total");
if (index >= 0 && index != list.Count - 1)
{
var total = list[index];
list.RemoveAt(index);
list.Add(total);
}
In the later case, if you don't really care about the order of the other items in the list, another more efficient way would be to simply exchange the total with the last element:
if (index >= 0 && index != list.Count - 1)
{
var total = list[index];
list[index] = list[list.Count - 1];
list[list.Count - 1] = total;
}