简体   繁体   English

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

[英]How to get Array Items inside of List<object> c#

How to get Array Items inside of List c# 如何获取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 来自Web服务的ArrayItems的图像

thanks in Advance! 提前致谢!

如果enumerable包含Brand对象,那么你需要的是enumerable.ElementAt(i)返回的元素:

var brnd = enumerable.ElementAt(i) as Brand;

I think your code can be a bit cleaner in this way: 我认为你的代码可以用这种方式更清洁:

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

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

The OfType only returns objects that have a runtime type Brand, and the result is an IEnumerable, the ToList() makes them into a fresh list. OfType仅返回运行时类型为Brand的对象,结果为IEnumerable,ToList()将它们变为新的列表。

        var enumerable = c1._getbrandrequest( Object[] );

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

        foreach (var brnd in enumerable)
        {
            Brand model = new Brand();

            model.sid = brnd.sid;

            brands.Add(model);

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM