How can I convert a List to an IEnumerable and back again.
The reason I want to do this is to run a series of LINQ statements on the List i.e. sort etc
How can I convert a List to an IEnumerable and back again.
The reason I want to do this is to run a series of LINQ statements on the List i.e. sort etc
List<string> myList = new List<string>();
IEnumerable<string> myEnumerable = myList;
List<string> listAgain = myEnumerable.ToList();
Aside: Note that the standard LINQ operators (as per the earlier example) don't change the existing list - list.OrderBy(...).ToList() will create a new list based on the re-ordered sequence. It is pretty easy, however, to create an extension method that allows you to use lambdas with List<T>.Sort:
static void Sort<TSource, TValue>(this List<TSource> list,
Func<TSource, TValue> selector)
{
var comparer = Comparer<TValue>.Default;
list.Sort((x,y) => comparer.Compare(selector(x), selector(y)));
}
static void SortDescending<TSource, TValue>(this List<TSource> list,
Func<TSource, TValue> selector)
{
var comparer = Comparer<TValue>.Default;
list.Sort((x,y) => comparer.Compare(selector(y), selector(x)));
}
Then you can use:
list.Sort(x=>x.SomeProp); // etc
This updates the existing list in the same way that List<T>.Sort usually does.
A List<T> is an IEnumerable<T>, so actually, there's no need to 'convert' a List<T> to an IEnumerable<T>.
Since a List<T> is an IEnumerable<T>, you can simply assign a List<T> to a variable of type IEnumerable<T>.
The other way around, not every IEnumerable<T> is a List<T> offcourse, so then you'll have to call the ToList() member method of the IEnumerable<T>.
A List<T> is already an IEnumerable<T>, so you can run LINQ statements directly on your List<T> variable.
If you don't see the LINQ extension methods like OrderBy() I'm guessing it's because you don't have a using System.Linq directive in your source file.
You do need to convert the LINQ expression result back to a List<T> explicitly, though:
List<Customer> list = ...
list = list.OrderBy(customer => customer.Name).ToList()