C# 使用 LINQ 将“ArrayList”转换为“List<string>”(或“List<T>”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11964877/
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
Convert 'ArrayList' to 'List<string>' (or 'List<T>') using LINQ
提问by Elvin
I want to convert an ArrayListto a List<string>using LINQ. I tried ToList()but that approach is not working:
我想使用 LINQ将 an 转换ArrayList为 a List<string>。我试过了,ToList()但这种方法不起作用:
ArrayList resultsObjects = new ArrayList();
List<string> results = resultsObjects.ToList<string>();
采纳答案by Jon Skeet
Your code actuallyshows a List<ArrayList>rather than a single ArrayList. If you're really got just one ArrayList, you'd probably want:
您的代码实际上显示了一个List<ArrayList>而不是单个ArrayList. 如果你真的只有一个ArrayList,你可能想要:
ArrayList resultObjects = ...;
List<string> results = resultObjects.Cast<string>()
.ToList();
The Castcall is required because ArrayListis weakly typed - it only implements IEnumerable, not IEnumerable<T>. Almost all the LINQ operators in LINQ to Objects are based on IEnumerable<T>.
该Cast调用是必需的,因为ArrayList是弱类型-它只是工具IEnumerable,不是IEnumerable<T>。LINQ to Objects 中的几乎所有 LINQ 运算符都基于IEnumerable<T>.
That's assuming the values within the ArrayListreally are strings. If they're not, you'll need to give us more information about how you want each item to be converted to a string.
那是假设里面的值ArrayList真的是字符串。如果不是,您需要向我们提供有关您希望如何将每个项目转换为字符串的更多信息。
回答by Daniel Hilgarth
I assume your first line was meant to be ArrayList resultsObjects = new ArrayList();.
我假设你的第一行是ArrayList resultsObjects = new ArrayList();.
If the objects inside the ArrayListare of a specific type, you can use the Cast<Type>extension method:
如果里面的对象ArrayList是特定类型的,你可以使用Cast<Type>扩展方法:
List<string> results = resultsObjects.Cast<string>().ToList();
If there are arbitrary objects in ArrayListwhich you want to convertto strings, you can use this:
如果有ArrayList要转换为字符串的任意对象,可以使用以下命令:
List<string> results = resultsObjects.Cast<object>().Select(x => x.ToString())
.ToList();
回答by adatapost
You can convert ArrayListelements to object[]array using ArrayList.ToArray()method.
您可以使用方法将ArrayList元素转换为object[]数组ArrayList.ToArray()。
List<ArrayList> resultsObjects = new List<ArrayList>();
resultsObjects.Add(new ArrayList() { 10, "BB", 20 });
resultsObjects.Add(new ArrayList() { "PP", "QQ" });
var list = (from arList in resultsObjects
from sr in arList.ToArray()
where sr is string
select sr.ToString()).ToList();
回答by Trevor
You can also use LINQ's OfType<> method, depending on whether you want to raise an exception if one of the items in your arrayList is not castable to the desired type. If your arrayList has objects in it that aren't strings, OfType() will ignore them.
您还可以使用 LINQ 的 OfType<> 方法,这取决于您是否希望在 arrayList 中的一项无法转换为所需类型时引发异常。如果您的 arrayList 中有不是字符串的对象,OfType() 将忽略它们。
var oldSchoolArrayList = new ArrayList() { "Me", "You", 1.37m };
var strings = oldSchoolArrayList.OfType<string>().ToList();
foreach (var s in strings)
Console.WriteLine(s);
Output:
输出:
Me
You

