c# - C#如何过滤类型T的列表以仅返回新数据
问题描述
我试图在我的系统中创建一个过滤器功能,这样我就可以调用这个函数来过滤重复项。经过 2 天的尝试,我感到很沮丧,因为我无法让它发挥作用。老实说,我什至不知道我做得对还是可能。
它的工作方式是我从另一个函数调用“过滤器”函数。过滤器函数有 2 个参数。1 是现有的项目列表。2 是包含新数据的列表。这就是我调用函数的方式
List<RdwDamageTable> Existing = await _context.RdwDamageTable.ToListAsync();
List<RdwDamageTable> Data = jsonArray.ToObject<List<RdwDamageTable>>();
List<RdwDamageTable> ToInsert = Sys.BaseFunctions.FilterDuplicate(Existing , Data);
Filter 函数现在看起来像这样。
public static List<T> FilterDuplicate<T>(List<T> Existing, List<T> New)
{
return null;
}
我再次想看看新列表是否包含现有没有的新项目,所以我可以返回一个新对象列表。
希望有人可以提供帮助。
解决方案
using System.Linq; // needed at the top somewhere
// ...
public static List<T> FilterDuplicate<T>(List<T> Existing, List<T> New)
=> New.Except(Existing).ToList();
推荐阅读
- kubernetes-helm - Helm 范围列表与元组
- excel - vba嵌套for循环
- python - Python将字节转换为str
- workflow - NetSuite 工作流“将结果存储在”中未列出任何字段
- mysql - 如何在多个组和表上使用 WHERE 子句获得前 3 位平均值?
- java - HMS Map Kit - 如何将相机缩放设置为当前位置?
- jquery - 使用 JQuery 在刀片文件中获取 Laravel Session 值
- python - 如何在填充了 0 或 1 个布尔值的 python 中创建邻接矩阵?
- arrays - 如何正确地将动态二维数组传递给 C 中的函数
- php - Laravel 8.0 看不到 Rollbar