c# - 将列表添加到实例化 C# 上的另一个列表
问题描述
如何将一个列表添加到另一个列表?也许我问的方式不对,但请耐心等待一分钟。我有一个列表,KeyValuePair<string, object>
我想将另一个列表成员添加到此列表中。像这样:
var list = new List<KeyValuePair<string, object>>{ new KeyValuePair<string, object>("Screw", "1"),
new KeyValuePair<string, object>("You", "2"),
new KeyValuePair<string, object>("Guys", "3")};
var secondList = new List<KeyValuePair<string, object>>{ list, // through error
new KeyValuePair<string, object>("I'm", "4"),
new KeyValuePair<string, object>("Going", "5"),
new KeyValuePair<string, object>("Home", "6") };
你可以猜到,错误是
无法从 'System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, object>>' 转换为 'System.Collections.Generic.KeyValuePair<string, object>'
list
在. _ secondList
_ secondList
那么我该如何实现呢?
解决方案
Concat 和 ToList LINQ 方法:
var secondList = list.Concat(new List<KeyValuePair<string, object>>{
new KeyValuePair<string, object>("I'm", "4"),
new KeyValuePair<string, object>("Going", "5"),
new KeyValuePair<string, object>("Home", "6") }).ToList();
推荐阅读
- javascript - Microsoft Bot Framework,从 Javascript 设置用户状态
- angular - 表单控件倒序角
- c++ - 标签大小不固定时如何处理前向引用
- javascript - 为什么我的 handleChange 没有被调用?MaterialUI
- android - 无法从意图中保存图片
- .net - 在实体框架中使用 AsQueryable 编写带有枚举值的 WHERE 条件
- node.js - 数据库重置后,Sequelize 错误枚举已存在
- optimization - Julia 优化达到最大迭代次数
- kdb - kdb:值和评估之间的差异
- pandas - Pandas 根据值替换多列的值