使用表达式/lambda 比较/过滤两个列表的通用方法
我想根据过滤表达式比较两个列表;不确定如何构造泛型方法的 lambda 表达式;请参考下面的代码;或者是否有通过 LINQ 中的相交更简单的方法?
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Data d1 = new Data {Id = 1, Name = "One"};
Data d2 = new Data { Id = 2, Name = "Two" };
Data d3 = new Data { Id = 3, Name = "Three" };
Data d4 = new Data { Id = 1, Name = "One" };
Data d5 = new Data { Id = 2, Name = "Two" };
Data d6 = new Data { Id = 4, Name = "Four" };
List<Data> original = new List<Data> {d1, d2, d3};
List<Data> filterItems = new List<Data> {d4, d5, d6};
List<Data> result = original.FilterDataList(filterItems);
//How to call this method?
List<Data> genericCall = original.FilterList<Data>(filterItems, data => data.Id ?????????????)
}
}
public class Data
{
public long Id;
public string Name;
}
public static class Extensions
{
public static List<Data> FilterDataList(this List<Data> sourceList, List<Data> filterOutItems)
{
return sourceList.Where(p => filterOutItems.All(l => l.Id != p.Id)).ToList();
}
public static List<T> FilterList<T>(this List<T> sourceList, List<T> filterOutItems, Func<T, bool> filterExpression)
{
return sourceList.Where(p => filterOutItems.All(filterExpression)).ToList();
}
}
}
I want to compare two lists, based on a filter expression; not sure how to construct the lambda expression for the generic method; Please refer to the code below; or is there an easier way via an intersect in LINQ?
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Data d1 = new Data {Id = 1, Name = "One"};
Data d2 = new Data { Id = 2, Name = "Two" };
Data d3 = new Data { Id = 3, Name = "Three" };
Data d4 = new Data { Id = 1, Name = "One" };
Data d5 = new Data { Id = 2, Name = "Two" };
Data d6 = new Data { Id = 4, Name = "Four" };
List<Data> original = new List<Data> {d1, d2, d3};
List<Data> filterItems = new List<Data> {d4, d5, d6};
List<Data> result = original.FilterDataList(filterItems);
//How to call this method?
List<Data> genericCall = original.FilterList<Data>(filterItems, data => data.Id ?????????????)
}
}
public class Data
{
public long Id;
public string Name;
}
public static class Extensions
{
public static List<Data> FilterDataList(this List<Data> sourceList, List<Data> filterOutItems)
{
return sourceList.Where(p => filterOutItems.All(l => l.Id != p.Id)).ToList();
}
public static List<T> FilterList<T>(this List<T> sourceList, List<T> filterOutItems, Func<T, bool> filterExpression)
{
return sourceList.Where(p => filterOutItems.All(filterExpression)).ToList();
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
感谢大家指出 LINQ except 扩展,这是我的最终解决方案
Thanks to everyone for pointing the LINQ Except extension out, here is my end solution
如果我正确理解您的问题,
FilterList
是FilterDataList
的通用版本,您在其中将 lambda 作为参数传递。在这种情况下,您将按如下方式调用该方法:如果您想使用 except,如 @ivancho 和 @perelman 建议的那样,您可以使用如下方法:
然后您将按如下方式调用它:
If I understand your question correctly,
FilterList
is a generic version ofFilterDataList
where you are passing in the lambda as a parameter. In that case you would call the method as follows:If you want to use Except as @ivancho and @perelman have suggested you could use a method like this:
You would then call it as follows:
您想要的输出是什么?您是否尝试过 https://www.google.com/search?q= 中的第一个结果linq+相交?看来您应该仔细阅读 Enumerable 文档 - 您正在使用 .All 您最有可能指的是 .Any,并且一般来说它会让您更好地了解 LINQ 的可能性。
What is your desired output? Did you try the first result in https://www.google.com/search?q=linq+intersect ? It seems like you should go through the Enumerable documentation - you are using .All where you most likely mean .Any, and just in general it would give you a better idea of what is possible with LINQ.
我不清楚你想做什么。您的
FilterDataList
似乎与Except()
.ToList()
。FilterList
中的.Where
不使用p
(lambda 的参数),所以我不清楚你想用过滤表达式。也许您正在寻找使用不同的IEqualityComparer
和Except()
,您必须将其定义为单独的类。I am not clear what you are trying to do. Your
FilterDataList
appears to be the same asExcept()
.ToList()
. The.Where
in yourFilterList
does not usep
(the argument to the lambda), so I am unclear what you want to do with the filter expression. Maybe you are looking for using a differentIEqualityComparer
withExcept()
which you would have to define as a separate class.