将数组转换为自定义对象列表C# [英] Convert Array to custom object list c#

查看:68
本文介绍了将数组转换为自定义对象列表C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组:-

private string[][] barValues = new string[][] { new string[]{ "1.9", "5.8", "4.8", "Since Inception", "24-Jan 2014 to 24 Jun 2014" },
                                                new string[]{"1.2", "16.5","9.8", "Year to date","01-Apr 2014 to 24-Jun 2014" }, 
                                                new string[]{"11.6","28.8","23.5","Last quarter","01-Jan to 24-Jun 2014"} };

我想将此数组转换为我的自定义列表:-

I want to convert this array into my custom list :-

List<Portfolio> list = new List<Portfolio>();

我尝试做:-

List<Portfolio> list=myArray.Cast<Portfolio>().ToList();

但是我得到一个错误:-

But I get a error:-


System.InvalidCastException:无法从源类型转换为
目标类型。

System.InvalidCastException: Cannot cast from source type to destination type.

怎么办我要进行这种转换吗?

How do I do this conversion?

推荐答案

您将需要使用Select运算符,并将字符串数组分配给Portfolio对象。像这样的东西:

You will need to use the Select operator and assign your array of strings to your Portfolio object. Something like this:

myArray.Select(array => new Portfolio { Field1 = array[0], Field2 = array[1] }).ToList()

这篇关于将数组转换为自定义对象列表C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆