c# - 如何对 List(Of Integer()) 进行区分?

标签 c# .net vb.net linq

自此

(vb.net)

    Dim test As New List(Of Integer())
    test.Add(New Integer() {1, 2, 3})
    test.Add(New Integer() {1, 3, 3})
    test.Add(New Integer() {3, 2, 3})
    test.Add(New Integer() {1, 1, 3})
    test.Add(New Integer() {1, 2, 3})
    Dim a = test.Distinct

(c#)

    List<int[]> test = new List<int[]>();
    test.Add(new int[] { 1, 2, 3 });
    test.Add(new int[] { 1, 3, 3 });
    test.Add(new int[] { 3, 2, 3 });
    test.Add(new int[] { 1, 1, 3 });
    test.Add(new int[] { 1, 2, 3 });
    var a = test.Distinct();

不工作,你会怎么做不同?

最佳答案

您必须为 Distinct 提供自定义相等比较器才能在这种情况下工作 - 否则您正在比较引用,这是一个初步尝试:

class SequenceComparer<T,U> : IEqualityComparer<T> where T: IEnumerable<U>
{
    public bool Equals(T x, T y)
    {
        return Enumerable.SequenceEqual(x, y);
    }

    public int GetHashCode(T obj)
    {
        int hash = 19;
        foreach (var item  in obj)
        {
            hash = hash * 31 + item.GetHashCode();
        }
        return hash;
    }
}

现在您可以在调用 Distinct() 时使用它:

var results = test.Distinct(new SequenceComparer<int[],int>())
                  .ToList();

关于c# - 如何对 List(Of Integer()) 进行区分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10922877/

相关文章:

c# - 网络爬虫解析 PHP/Javascript 链接?

.net - MSBuild (TFS) 中的 Nhibernate.Bytecode.CaSTLe 问题

c# - 有没有直接的方法将 Size 或 Point 乘以一个数字?

vb.net - vb.net 表单的 friend 与公众

mysql - VB.NET使定时器作为基于数据库数据表时间的响应时间

c# - 从通用 Windows 平台 (UWP) 应用程序向 Azure 通知中心发送通知

c# - 如何从创建它的代码中获取对 WCF 服务对象的引用?

JavaScript 跳过 AJAX Post

vb.net - vb.net(在Visual Studio 2013中制造)和Access DB的几个问题

c# - 如何格式化带有千位分隔符和小数点逗号的字符串?