从字符串数组列表转换为对象列表 [英] Convert from List of string array to List of objects

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

问题描述

如果我有一个像这样的简单类:

If I have a simple class that looks like this:

public string Param1 { get; set; }
public string Param2 { get; set; }
public SimpleClass (string a, string b) { Param1 = a; Param2 = b; }

从另一个类返回的字符串数组列表:

List of string array returned from another class:

var list = new List<string[]> {new[] {"first", "second"}, new[] {"third", "fourth"}};

有没有一种更有效的方式使用C#以List<SimpleClass>结尾而不做类似的事情:

Is there a more efficient way using C# to end up with List<SimpleClass> without doing something like:

var list1 = new List<SimpleClass>();
foreach (var i in list)
{          
    var data = new SimpleClass(i[0], i[1]);
    list1.Add(data);         
}

推荐答案

您可以使用Linq:

var simpleClassList = originalList.Select(x => new SimpleClass(x[0], x[1])).ToList()

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

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