c# - 如何在 List<object> c# 中获取数组项

标签 c# arraylist ienumerable

如何获取List c#中的数组项

IEnumerable<object> enumerable = c1._getbrandrequest(object[]);

List<object> brand = enumerable.Cast<object>().ToList();

List<Brand> brands = new List<Brand>();

for (int i = 0; i <= enumerable.Count(); i++)
{

    var brnd = enumerable.ElementAt(i);

    // brnd (intellisense only produce)
    // brnd. { Equals, GetHashCode, GetType, ToString }

}

Image of ArrayItems from Web Services

提前致谢!

最佳答案

我认为您的代码可以通过这种方式更简洁一些:

IEnumerable<object> enumerable = c1._getbrandrequest(object[]);
List<Brand> brands = enumerable.OfType<Brand>().ToList();

foreach (Brand brnd in brands)
{
  // Do things with brnd.
}

OfType 只返回具有运行时类型 Brand 的对象,结果是一个 IEnumerable,ToList() 将它们放入一个新列表。

关于c# - 如何在 List<object> c# 中获取数组项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35611221/

相关文章:

java - 为什么我不能在 arraylist 中使用 getFixed 方法?我怎样才能让它发挥作用?

java - 包含移动圆圈的 ArrayList

java - 清除 ArrayList 会导致此 ArrayList 中的数据传递到类构造函数也被清除

c# - 从 System.Collections.Generic.IEnumerable 中获取前 n 个元素

c# - Razor 中的类型检查

c# - 使用 C# 检测二维空间中的光投影和交叉点

c# - 将 List<int> 转换为 List<List<int>>

c# - IEqualityComparer 和 Contains 方法

c# - IEnumerable 集合在 WCF 传输时被清除

c# - 如何获取条件为字符串数组的记录