c# - 将列表分配给自身并与另一个列表连接

标签 c# concatenation

为什么这有效

this.radGridViewFiles.DataSource = null;
this.radGridViewFiles.DataSource = MyGlobals.ListOfItemsToControl.Concat(MyGlobals.lstNewItems.Where(i => i.sItemRequestStatus == "Add").ToList()); 

但这给了我下面的错误

MyGlobals.ListOfItemsToControl =  MyGlobals.ListOfItemsToControl.Concat(MyGlobals.lstNewItems.Where(i => i.sItemRequestStatus == "Add").ToList());

“无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“System.Collections.Generic.List”。存在显式转换(是否缺少强制转换?)”

最佳答案

您不需要使用ToListWhere因为Concat需要IEnumerable<TSource>作为参数(这是 Where 的返回类型,无需转换为 List<TSource> ),而是在语句末尾使用它并将返回结果转换为 List<T>这是 IEnumerable<T>

MyGlobals.ListOfItemsToControl.Concat(MyGlobals.lstNewItems.Where(i => i.sItemRequestStatus == "Add")).ToList();

这是有效的,因为 DataSource 的类型是 object最有可能。因此接受 IEnumerable<T>List<T> or any other type.

this.radGridViewFiles.DataSource = MyGlobals.ListOfItemsToControl.Concat(MyGlobals.lstNewItems.Where(i => i.sItemRequestStatus == "Add").ToList()); 

关于c# - 将列表分配给自身并与另一个列表连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21510569/

相关文章:

c# - 您使用哪个 .NET Memcached 客户端,EnyimMemcached 与 BeITMemcached?

list - Erlang List 连接,奇怪的 "|"符号

c# - SignalR:生成的代理与动态创建的集线器文件

c# - 为什么这个带有可空值的 C# 重载没有歧义?

如何使用 AsParallel()/Parellel.ForEach() 的 C# 编码指南

Python:截断句子的最后一个单词?

video - 使用 ffmpeg 和 xfade 过滤器合并多个视频文件

Excel "UnCONCATENATE"/函数中爆炸/将单元格转换为数组

video - 如何连接具有不同属性的ffmpeg中的视频?

c# - SQL Server 中的 varbinary 类型如何在 C# 中的函数中使用