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

时间:2016-02-24 19:26:02

标签: 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

提前致谢!

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:1)

我认为你的代码可以用这种方式更清洁:

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()将它们变成一个新的列表。

答案 2 :(得分:0)

return [
    // ...
    'components' => [
        'response' => [
            'class' => 'yii\web\Response',
            'on beforeSend' => function ($event) {
                $response = $event->sender;
                if ($response->data !== null && Yii::$app->request->get('suppress_response_code')) {
                    $response->data = [
                        'success' => $response->isSuccessful,
                        'data' => $response->data,
                    ];
                    $response->statusCode = 200;
                }
            },
        ],
    ],
];