问题描述
我有两个模型列表,我想添加到第三个模型列表中,但出现错误-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();