How can I convert a List<MyObject>
to an IEnumerable<MyObject>
and then back again?
I want to do this in order to run a series of LINQ statements on the List, e. g. Sort()
We can use the AsEnumerable() function of LINQ to convert a list to an IEnumerable in C#.
You can use the extension method AsEnumerable in Assembly System. Core and System. Linq namespace : List<Book> list = new List<Book>(); return list.
Is IEnumerable faster than List? IEnumerable is conceptually faster than List because of the deferred execution. Deferred execution makes IEnumerable faster because it only gets the data when needed. Contrary to Lists having the data in-memory all the time.
List<string> myList = new List<string>(); IEnumerable<string> myEnumerable = myList; List<string> listAgain = myEnumerable.ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With