104

如何将 a 转换List<MyObject>为 anIEnumerable<MyObject>然后再返回?

我想这样做是为了在列表上运行一系列 LINQ 语句,例如Sort()

4

6 回答 6

148
List<string> myList = new List<string>();
IEnumerable<string> myEnumerable = myList;
List<string> listAgain = myEnumerable.ToList();
于 2009-01-23T12:08:20.007 回答
21

AList<T>是 an IEnumerable<T>,所以实际上,没有必要将 a '转换'List<T>为 an IEnumerable<T>。由于 aList<T>是一个IEnumerable<T>,您可以简单地将 a 分配给List<T>一个类型为 的变量IEnumerable<T>

反过来说,不是每一个IEnumerable<T>都是List<T>offcourse,所以你必须ToList()调用IEnumerable<T>.

于 2009-01-23T13:15:48.480 回答
9

AList<T>已经是IEnumerable<T>,因此您可以直接在List<T>变量上运行 LINQ 语句。

如果您没有看到像OrderBy()我猜的那样的 LINQ 扩展方法,那是因为您的源文件中没有using System.Linq指令。

但是,您确实需要将 LINQ 表达式结果显式转换回List<T>

List<Customer> list = ...
list = list.OrderBy(customer => customer.Name).ToList()
于 2010-02-17T12:33:38.263 回答
5

另外:请注意,标准 LINQ 运算符(根据前面的示例)不会更改现有列表 -list.OrderBy(...).ToList()将根据重新排序的序列创建一个新列表。然而,创建一个允许您使用 lambdas 的扩展方法非常容易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)));
}

然后你可以使用:

list.Sort(x=>x.SomeProp); // etc

这会以与通常相同的方式更新现有列表。List<T>.Sort

于 2009-01-23T13:09:06.980 回答
1

转换List<T>IEnumerable<T>

List<T>实现IEnumerable<T>(以及许多其他,例如IList<T>, ICollection<T>),因此无需将 List 转换回 IEnumerable,因为它已经是IEnumerable<T>.

例子:

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

Person person1 = new Person() { Id = 1, Name = "Person 1" };
Person person2 = new Person() { Id = 2, Name = "Person 2" };
Person person3 = new Person() { Id = 3, Name = "Person 3" };

List<Person> people = new List<Person>() { person1, person2, person3 };

//Converting to an IEnumerable
IEnumerable<Person> IEnumerableList = people;

你也可以使用Enumerable.AsEnumerable()方法

IEnumerable<Person> iPersonList = people.AsEnumerable();

转换IEnumerable<T>List<T>

IEnumerable<Person> OriginallyIEnumerable = new List<Person>() { person1, person2 };
List<Person> convertToList = OriginallyIEnumerable.ToList();

这在Entity Framework中很有用。

于 2016-11-07T10:18:40.297 回答
0

为了防止内存重复,resharper 建议这样做:

List<string> myList = new List<string>();
IEnumerable<string> myEnumerable = myList;
List<string> listAgain = myList as List<string>() ?? myEnumerable.ToList();

.ToList() 返回一个新的不可变列表。因此,对 listAgain 的更改不会影响@Tamas Czinege 答案中的 myList 。在大多数情况下这是正确的,至少有两个原因:这有助于防止一个区域的更改影响另一个区域(松散耦合),并且它非常可读,因为我们不应该在设计代码时考虑编译器问题。

但是在某些情况下,例如处于紧密循环中或在嵌入式或低内存系统上工作,应该考虑编译器的考虑。

于 2014-08-28T10:20:41.330 回答