如何解决将模型列表添加到另一个模型列表
我有两个模型列表,我想添加到第三个模型列表中,但出现错误-No overload for method "Add" takes two arguments
。
这是我的代码:
List<TableNotificationModel> one = JsonConvert.DeserializeObject<List<TableNotificationModel>>(cmdPositive);
List<TableNotificationModel> two = JsonConvert.DeserializeObject<List<TableNotificationModel>>(cmdNegative);
List<TableNotificationModel> three = new List<TableNotificationModel>();
three.Add(one,two);
IEnumerable<TableNotificationModel> result = three;
DataSourceResult dataSource = result.ToDataSourceResult(request);
我想将列表result
设置为three
的值,以便在下面使用它。
我尝试使用AddRange
而不是Add
,但是会引发相同的错误。
我实习的一些开发人员告诉我研究Dictionary
,但我
无法弄清楚如何用字典来解决这个问题。
我尝试了类似three.Add(one)
这样的操作,但提示为cannot convert generic list to model
。
有关如何进行的任何建议?
解决方法
您应该使用AddRange()
。
Add
接受单个对象,而AddRange()
接受IEnumerable
个对象。
所以,
three.AddRange(one);
如果这应该只是项目(联合)的平面列表:
var one = JsonConvert.DeserializeObject<List<TableNotificationModel>>(cmdPositive);
var two = JsonConvert.DeserializeObject<List<TableNotificationModel>>(cmdNegative);
var three = one.Union(two).ToList();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。