Linq Distinct 具有单个比较类(和接口)

发布于 2024-10-09 18:07:34 字数 1008 浏览 0 评论 0原文

我的应用程序中有几个类,所有这些类都有一个 Name 属性,我想将其用作比较的基础(Distinct() 等)。由于我总是要比较 Name,所以我决定提取一个接口 ISomeComparedStuff,它只具有一个 Name 属性,我的所有其他类实现。我这样设置了一个比较类:

public class MyComparer : IEqualityComparer<ISomeComparedStuff>
{
     public bool Equals(ISomeComparedStuff x, ISomeComparedStuff y)
     {
          return x.Name == y.Name;
     }

     public int GetHashCode(ISomeComparedStuff obj)
     {
          return obj.Name.GetHashCode();
     }
}

唯一的问题是当我尝试针对它进行编码时:

public class SomeStuff : ISomeComparedStuff
{
  ...
}

public class SomeMoreStuff : ISomeComparedStuff
{
  ...
}

var someStuff = GetSomeStuff().Distinct(new MyComparer);
var someMoreStuff = GetSomeMoreStuff().Distinct(new MyComparer);

我收到一个强制转换错误(SomeStuffISomeComparedStuff)。有没有办法做到这一点,这样我只需要一个比较类,否则我必须为每个类创建一个比较类(即使我总是要比较Name)?

注意:我理解这个问题“标题”需要帮助。任何建议都会很棒。

I have several classes in my application, all of which have a Name property that I want to use as my basis for comparison (Distinct(), etc.). Since I am always going to be comparing on Name, I decided to extract an interface, ISomeComparedStuff, which simply has a Name propery that all my other classes implement. I set up a comparison class as such:

public class MyComparer : IEqualityComparer<ISomeComparedStuff>
{
     public bool Equals(ISomeComparedStuff x, ISomeComparedStuff y)
     {
          return x.Name == y.Name;
     }

     public int GetHashCode(ISomeComparedStuff obj)
     {
          return obj.Name.GetHashCode();
     }
}

The only problem is when I try to code against it:

public class SomeStuff : ISomeComparedStuff
{
  ...
}

public class SomeMoreStuff : ISomeComparedStuff
{
  ...
}

var someStuff = GetSomeStuff().Distinct(new MyComparer);
var someMoreStuff = GetSomeMoreStuff().Distinct(new MyComparer);

I am getting a cast error (SomeStuff to ISomeComparedStuff). Is there some way to do this so I only need one compare class, otherwise I'd have to create one for every one of my classes (even though I am always going to compare on Name)?

Note: I understand this question "title" needs help. Any suggestions would be great.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难忘№最初的完美 2024-10-16 18:07:34

不确定这是否是一个好的解决方案,但是让 MyComparer 成为一个泛型类怎么样?

public class MyComparer<T> : IEqualityComparer<T>
    where T: ISomeComparedStuff
{
     public bool Equals(T x, T y)
     {
      return x.Name == y.Name;
     }

     public int GetHashCode(T obj)
     {
      return obj.Name.GetHashCode();
     }
}

缺点是你必须更新一个合适的版本:

var someStuff = GetSomeStuff().Distinct(new MyComparer<SomeStuff>());
var someMoreStuff = GetSomeMoreStuff().Distinct(new MyComparer<SomeMoreStuff>());

扩展一点,你也可以创建一个新的扩展方法,如下所示:

public static IEnumerable<T> DistinctByName<T>(this IEnumerable<T> values)
    where T: ISomeComparedStuff
{
    return values.Distinct(new MyComparer<T>());
}

Not sure if this is a good solution or not, but how about making MyComparer a generic class?

public class MyComparer<T> : IEqualityComparer<T>
    where T: ISomeComparedStuff
{
     public bool Equals(T x, T y)
     {
      return x.Name == y.Name;
     }

     public int GetHashCode(T obj)
     {
      return obj.Name.GetHashCode();
     }
}

Downside is you have to new up an appropriate version:

var someStuff = GetSomeStuff().Distinct(new MyComparer<SomeStuff>());
var someMoreStuff = GetSomeMoreStuff().Distinct(new MyComparer<SomeMoreStuff>());

Expanding a bit, you could also make a new extension method like this:

public static IEnumerable<T> DistinctByName<T>(this IEnumerable<T> values)
    where T: ISomeComparedStuff
{
    return values.Distinct(new MyComparer<T>());
}
如痴如狂 2024-10-16 18:07:34

也许是这样的:

var someStuff = GetSomeStuff().Cast<ISomeComparedStuff>().Distinct(new MyComparer);

或者使用非通用的 IEqualityComparer

Maybe something like:

var someStuff = GetSomeStuff().Cast<ISomeComparedStuff>().Distinct(new MyComparer);

Or use the non-generic IEqualityComparer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文