c#中的Select和ConvertAll之间的区别

时间:2022-09-05 11:50:59

I have some List:

我有一些列表:

List<int> list = new List<int> { 1, 2, 3, 4, 5 };

I want to apply some transformation to elements of my list. I can do this in two ways:

我想对列表中的元素应用一些变换。我可以用两种方式做到:

List<int> list1 = list.Select(x => 2 * x).ToList();
List<int> list2 = list.ConvertAll(x => 2 * x).ToList();

What is the difference between these two ways?

这两种方法的区别是什么?

3 个解决方案

#1


93  

Select is a LINQ extension method and works on all IEnumerable<T> objects whereas ConvertAll is implemented only by List<T>. The ConvertAll method exists since .NET 2.0 whereas LINQ was introduced with 3.5.

Select是一种LINQ扩展方法,适用于所有IEnumerable 对象,而ConvertAll仅通过List 实现。自。net 2.0以来就有了ConvertAll方法,而LINQ是在3.5引入的。

You should favor Select over ConvertAll as it works for any kind of list, but they do the same basically.

您应该选择Select,而不是ConvertAll,因为它适用于任何类型的列表,但它们基本上是相同的。

#2


71  

ConvertAll is not an extension, it's a method in the list class. You don't have to call ToList on the result as it's already a list:

ConvertAll不是一个扩展,它是list类中的一个方法。你不需要在结果上调用ToList,因为它已经是一个列表:

List<int> list2 = list.ConvertAll(x => 2 * x);

So, the difference is that the ConvertAll method only can be used on a list, and it returns a list. The Select method can be used on any collection that implements the IEnumerable<T> interface, and it returns an IEnumerable<T>.

不同的是,ConvertAll方法只能在列表中使用,它返回一个列表。Select方法可以用于任何实现IEnumerable 接口的集合,它返回一个IEnumerable

Also, they do the processing differently, so they have their strengths in different situations. The ConvertAll method runs through the list and creates a new list in one go, while the Select method uses lazy execution and only processes the items as you need them. If you don't need all the item, the Select method is more efficient. On the other hand, once ConvertAll has returned the list, you don't need to keep the original list.

而且,它们处理的方式不同,所以它们在不同的情况下有各自的优势。ConvertAll方法在列表中运行并一次创建一个新列表,而Select方法使用惰性执行,只在需要时处理条目。如果您不需要所有的项,那么Select方法会更有效。另一方面,一旦ConvertAll返回列表,则不需要保留原始列表。

#3


1  

I know this is bit late but i have still added because this could be of some use for others in future.

我知道这有点晚了,但我还是补充了,因为这可能对以后的其他人有用处。

When using it in EntityFramework query expression it is not recommended to use ConvertAll() as it evaluates the expression rather than leaving it as expression for future use. This seriously degrades database query execution performance as it would have to make number of calls before evaluating final expression.

当在EntityFramework查询表达式中使用它时,不建议在计算表达式时使用ConvertAll(),而不要将它作为表达式留作将来使用。这严重地降低了数据库查询执行性能,因为在评估最终表达式之前,它必须进行多次调用。

#1


93  

Select is a LINQ extension method and works on all IEnumerable<T> objects whereas ConvertAll is implemented only by List<T>. The ConvertAll method exists since .NET 2.0 whereas LINQ was introduced with 3.5.

Select是一种LINQ扩展方法,适用于所有IEnumerable 对象,而ConvertAll仅通过List 实现。自。net 2.0以来就有了ConvertAll方法,而LINQ是在3.5引入的。

You should favor Select over ConvertAll as it works for any kind of list, but they do the same basically.

您应该选择Select,而不是ConvertAll,因为它适用于任何类型的列表,但它们基本上是相同的。

#2


71  

ConvertAll is not an extension, it's a method in the list class. You don't have to call ToList on the result as it's already a list:

ConvertAll不是一个扩展,它是list类中的一个方法。你不需要在结果上调用ToList,因为它已经是一个列表:

List<int> list2 = list.ConvertAll(x => 2 * x);

So, the difference is that the ConvertAll method only can be used on a list, and it returns a list. The Select method can be used on any collection that implements the IEnumerable<T> interface, and it returns an IEnumerable<T>.

不同的是,ConvertAll方法只能在列表中使用,它返回一个列表。Select方法可以用于任何实现IEnumerable 接口的集合,它返回一个IEnumerable

Also, they do the processing differently, so they have their strengths in different situations. The ConvertAll method runs through the list and creates a new list in one go, while the Select method uses lazy execution and only processes the items as you need them. If you don't need all the item, the Select method is more efficient. On the other hand, once ConvertAll has returned the list, you don't need to keep the original list.

而且,它们处理的方式不同,所以它们在不同的情况下有各自的优势。ConvertAll方法在列表中运行并一次创建一个新列表,而Select方法使用惰性执行,只在需要时处理条目。如果您不需要所有的项,那么Select方法会更有效。另一方面,一旦ConvertAll返回列表,则不需要保留原始列表。

#3


1  

I know this is bit late but i have still added because this could be of some use for others in future.

我知道这有点晚了,但我还是补充了,因为这可能对以后的其他人有用处。

When using it in EntityFramework query expression it is not recommended to use ConvertAll() as it evaluates the expression rather than leaving it as expression for future use. This seriously degrades database query execution performance as it would have to make number of calls before evaluating final expression.

当在EntityFramework查询表达式中使用它时,不建议在计算表达式时使用ConvertAll(),而不要将它作为表达式留作将来使用。这严重地降低了数据库查询执行性能,因为在评估最终表达式之前,它必须进行多次调用。