Is there a LINQ way to swap the position of two items inside a list?
Check the answer from Marc from C#: Good/best implementation of Swap method.
public static void Swap(IList list, int indexA, int indexB)
{
T tmp = list[indexA];
list[indexA] = list[indexB];
list[indexB] = tmp;
}
which can be linq-i-fied like
public static IList Swap(this IList list, int indexA, int indexB)
{
T tmp = list[indexA];
list[indexA] = list[indexB];
list[indexB] = tmp;
return list;
}
var lst = new List() { 8, 3, 2, 4 };
lst = lst.Swap(1, 2);