C#中Select和ConvertAll的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1571819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:58:26  来源:igfitidea点击:

Difference between Select and ConvertAll in C#

c#.netlist

提问by AndreyAkinshin

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?

这两种方式有什么区别?

采纳答案by Oliver Hanappi

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

Select是一个 LINQ 扩展方法,适用于所有IEnumerable<T>对象,而ConvertAll仅由List<T>. 该ConvertAll方法自 .NET 2.0 以来就存在,而 LINQ 是在 3.5 中引入的。

You should favor Selectover ConvertAllas it works for any kind of list, but they do the same basically.

你应该喜欢它SelectConvertAll因为它适用于任何类型的列表,但它们基本上都是一样的。

回答by Guffa

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

ConvertAll不是扩展,它是列表类中的一个方法。您不必调用ToList结果,因为它已经是一个列表:

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

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

因此,不同之处在于该ConvertAll方法只能用于列表,并且它返回一个列表。该Select方法可用于实现该IEnumerable<T>接口的任何集合,并返回一个IEnumerable<T>.

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

此外,它们的处理方式不同,因此它们在不同情况下各有优势。该ConvertAll方法遍历列表并一次性创建一个新列表,而该Select方法使用延迟执行并且仅在您需要时处理它们。如果您不需要所有项目,则该Select方法更有效。另一方面,一旦ConvertAll返回列表,您就不需要保留原始列表。

回答by Navap

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() ,因为它会计算表达式而不是将其保留为表达式以备将来使用。这会严重降低数据库查询执行性能,因为在评估最终表达式之前必须进行多次调用。

回答by Wesner Moise

The first answer should not be the accepted one. I am a former 2007 C# Microsoft MVP.

第一个答案不应该是公认的。我是前 2007 C# Microsoft MVP。

In contrast to the accepted response, ConvertAllis much more efficient than the combination of Selectand ToList().

与此相反的接受响应,ConvertAll比的组合更有效SelectToList()

First of all, ConvertAllis strictly faster and it uses the minimum amount of memory to do so. Same as Array.ConvertAll vs Select and ToArray. This would be a much more evident with a larger length array or many calls within a loop.

首先,ConvertAll绝对更快,它使用最少的内存来做到这一点。与 Array.ConvertAll 与 Select 和 ToArray 相同。对于更大长度的数组或循环中的许多调用,这将更加明显。

1) ConvertAllknows the size of the final list and avoids reallocating the base array. ToList() will keep resizing the array multiple times.

1)ConvertAll知道最终列表的大小并避免重新分配基数组。ToList() 将多次调整数组大小。

2) ToListwill make slower interface IEnumerable<>calls, while ConvertAllwill loop through the underlying array without extra calls or range checks.

2)ToList将进行较慢的接口IEnumerable<>调用,同时ConvertAll将遍历底层数组而无需额外调用或范围检查。

3) Select will create an extra IEnumerable<T>object.

3) Select 将创建一个额外的IEnumerable<T>对象。