首页 > 解决方案 > 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;
}

我再次想看看新列表是否包含现有没有的新项目,所以我可以返回一个新对象列表。

希望有人可以提供帮助。

标签: c#listfilter

解决方案


using System.Linq; // needed at the top somewhere
// ...
public static List<T> FilterDuplicate<T>(List<T> Existing, List<T> New)
    => New.Except(Existing).ToList();

推荐阅读