Freely convert between List and IEnumerable

前端 未结 6 962
逝去的感伤
逝去的感伤 2020-12-04 08:21

How can I convert a List to an IEnumerable and then back again?

I want to do this in order to run a series

6条回答
  •  Happy的楠姐
    2020-12-04 08:37

    A List is already an IEnumerable, so you can run LINQ statements directly on your List 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 explicitly, though:

    List list = ...
    list = list.OrderBy(customer => customer.Name).ToList()
    

提交回复
热议问题