我可以使用LINQ的除()与拉姆达EX pression比较器?可以使用、拉姆、pression、LINQ

2023-09-03 03:13:50 作者:酒自斟

我知道我可以调用LINQ的除并指定自定义的IEqualityComparer,但实现一个新的比较器类为每个数据类型似乎是一种矫枉过正为此目的。我可以用一个lambda EX pression提供平等的功能,当我用在哪里或者其他LINQ功能,如?

I know I can call linq's Except and specify a custom IEqualityComparer, but implementing a new Comparer class for each data type seems like an overkill for this purpose. Can I use a lambda expression to provide the equality function, like when I use Where or other LINQ functions?

如果我不能,是否有别的选择吗?

If I can't, is there an alternative?

推荐答案

我不认为你可以直接与基本的LINQ接口,但我见过人们实现一个LambdaComparer类扩展方法,这将帮助你做到这一点

I don't think you can directly with the basic LINQ interfaces, but I've seen people implement a LambdaComparer class with extension methods which will help you do it.

Here's一个例子

 
精彩推荐