C#:如何将2个列表合并为一个2元组列表 [英] C#: How do i get 2 lists into one 2-tuple list in
本文介绍了C#:如何将2个列表合并为一个2元组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个列表.第一个是Type string
.第二种是 object
类型.现在,我想将两个列表都放入 Tuple< string,object>
中.像这样
I have 2 Lists. First one is Type string
. The second is type object
. Now I want to get both lists into a Tuple<string,object>
.
like this
var List = new(string list1, object list2)[]
我该怎么做?
我必须创建2个单独的列表,因为我正在序列化对象,并且如果我首先具有 List< Tuple< string,object>>
,则序列化将无法进行.两个列表都可能变大,所以也许有foreach循环?
I had to create 2 seperate lists, because I am Serializing the object and the serializing wouldn't work if i had a List<Tuple<string,object>>
in the first place.
Both lists can get big so maybe with foreach loop?
推荐答案
You can use the Zip
method to create one list of the two:
var lst = new List<string>() { "a", "b" };
var obj = new List<object>() { 1, 2 };
var result = lst.Zip(obj, (x, y) => new Tuple<string, object>(x, y))
.ToList();
这篇关于C#:如何将2个列表合并为一个2元组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文