将模型列表添加到另一个模型列表

问题描述

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

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...