C# 将 List<int> 转换为 List<double>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2084493/
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 23:23:29  来源:igfitidea点击:

C# Converting List<int> to List<double>

c#generics

提问by Ames

I have a List<int>and I want to convert it to a List<double>. Is there any way to do this other than just looping through the List<int>and adding to a new List<double>like so:

我有一个List<int>,我想将它转换为一个List<double>. 除了像这样循环List<int>并添加到新的之外,还有什么方法可以做到这List<double>一点:

List<int> lstInt = new List<int>(new int[] {1,2,3});
List<double> lstDouble = new List<double>(lstInt.Count);//Either Count or Length, I don't remember

for (int i = 0; i < lstInt.Count; i++)
{
    lstDouble.Add(Convert.ToDouble(lstInt[0]));
}

Is there a fancy way to do this? I'm using C# 4.0, so the answer may take advantage of the new language features.

有没有一种奇特的方法来做到这一点?我使用的是 C# 4.0,所以答案可能会利用新的语言功能。

采纳答案by Guffa

You can use LINQ methods:

您可以使用 LINQ 方法:

List<double> doubles = integers.Select<int, double>(i => i).ToList();

or:

或者:

List<double> doubles = integers.Select(i => (double)i).ToList();

Also, the list class has a ForEach method:

此外,列表类有一个 ForEach 方法:

List<double> doubles = new List<double>(integers.Count);
integers.ForEach(i => doubles.Add(i));

回答by Darin Dimitrov

You could do this using the Selectextension method:

您可以使用Select扩展方法执行此操作:

List<double> doubleList = intList.Select(x => (double)x).ToList();

回答by Jon Skeet

You canuse Selectas suggested by others, but you can also use ConvertAll:

可以Select按照其他人的建议使用,但您也可以使用ConvertAll

List<double> doubleList = intList.ConvertAll(x => (double)x);

This has two advantages:

这有两个好处:

  • It doesn't require LINQ, so if you're using .NET 2.0 and don't want to use LINQBridge, you can still use it.
  • It's more efficient: the ToListmethod doesn't know the size of the result of Select, so it may need to reallocate buffers as it goes. ConvertAllknows the source and destination size, so it can do it all in one go. It can also do so without the abstraction of iterators.
  • 它不需要 LINQ,所以如果您使用 .NET 2.0 并且不想使用LINQBridge,您仍然可以使用它。
  • 它更有效:该ToList方法不知道 的结果的大小Select,因此它可能需要在执行过程中重新分配缓冲区。ConvertAll知道源和目标大小,因此它可以一次性完成所有操作。它也可以在没有迭代器抽象的情况下做到这一点。

The disadvantages:

缺点:

  • It only works with List<T>and arrays. If you get a plain IEnumerable<T>you'll have to use Selectand ToList.
  • If you're using LINQ heavily in your project, it may be more consistent to keep using it here as well.
  • 它只适用于List<T>和 数组。如果你得到一个平原,IEnumerable<T>你将不得不使用Selectand ToList
  • 如果您在项目中大量使用 LINQ,那么在这里继续使用它可能会更加一致。

回答by Giorgi

You can use Select or ConvertAll. Keep in mind that ConvertAll is available in .Net 2.0 too

您可以使用 Select 或 ConvertAll。请记住,ConvertAll 在 .Net 2.0 中也可用

回答by fyasar

You can use ConvertAll method inside of .Net Framework 2.0 here is an example

您可以在 .Net Framework 2.0 中使用 ConvertAll 方法,这里是一个示例

        List<int> lstInt = new List<int>(new int[] { 1, 2, 3 });
        List<double> lstDouble = lstInt.ConvertAll<double>(delegate(int p)
        {
            return (double)p;
        });

回答by Tom

You can use a method group:

您可以使用方法组:

lstDouble = lstInt.Select(Convert.ToDouble)