2

我在使用 except() 方法时遇到问题。它不返回差值,而是返回原始集合。

我尝试在 Account 类中实现 IEquatable 和 IEqualityComparer。我还尝试为 Account 创建一个单独的 IEqualityComparer 类。

当从 main 调用 except() 方法时,它似乎没有调用我自定义的 Equals() 方法,但是当我尝试 Count() 时,它确实调用了自定义 GetHashCode() 方法!

我确定我在某个地方犯了一个小错误,我希望一双新的眼睛可以帮助我。

主要的:

IEnumerable<Account> everyPartnerID = 
    from partner in dataContext.Partners
    select new Account { IDPartner = partner.ID, Name = partner.Name };


IEnumerable<Account> hasAccountPartnerID = 
    from partner in dataContext.Partners
    from account in dataContext.Accounts
    where
        !partner.ID.Equals(Guid.Empty) &&
        account.IDPartner.Equals(partner.ID) &&
        account.Username.Equals("Special")
    select new Account { IDPartner = partner.ID, Name = partner.Name };

IEnumerable<Account> noAccountPartnerID = 
    everyPartnerID.Except(
        hasAccountPartnerID, 
        new LambdaComparer<Account>((x, y) => x.IDPartner.Equals(y.IDPartner)));

帐户:

    public class Account : IEquatable<Account>
    {
        public Guid IDPartner{ get; set; }
        public string Name{ get; set; }

/*        #region IEquatable<Account> Members

        public bool Equals(Account other)
        {
            return this.IDPartner.Equals(other.IDPartner);
        }

        #endregion*/
    }

Lambda比较器:

   public class LambdaComparer<T> : IEqualityComparer<T>
    {
        private readonly Func<T, T, bool> _lambdaComparer;
        private readonly Func<T, int> _lambdaHash;

        public LambdaComparer(Func<T, T, bool> lambdaComparer) :
            this(lambdaComparer, o => o.GetHashCode())
        {
        }

        public LambdaComparer(Func<T, T, bool> lambdaComparer, Func<T, int> lambdaHash)
        {
            if (lambdaComparer == null)
                throw new ArgumentNullException("lambdaComparer");
            if (lambdaHash == null)
                throw new ArgumentNullException("lambdaHash");

            _lambdaComparer = lambdaComparer;
            _lambdaHash = lambdaHash;
        }

        public bool Equals(T x, T y)
        {
            return _lambdaComparer(x, y);
        }

        public int GetHashCode(T obj)
        {
            return _lambdaHash(obj);
        }
    }
4

4 回答 4

8

基本上LambdaComparer,当您只传入一个函数时,您的类就被破坏了,因为如果您不提供其他任何东西,它就会使用“身份哈希码”提供程序。哈希码由 使用Except,这就是导致问题的原因。

这里有三个选项:

  1. 实现您自己的ExceptBy方法,然后最好将其贡献给包含此类内容的MoreLINQ 。

  2. 使用不同的实现IEqualityComparer<T>。我有一个ProjectionEqualityComparer可以在MiscUtil中使用的类,或者您可以使用另一个问题中发布的代码。

  3. 将 lambda 表达式传递到您的LambdaComparer代码中以用于哈希:

    new LambdaComparer<Account>((x, y) => x.IDPartner.Equals(y.IDPartner)),
                                x => x.IDPartner.GetHashCode());
    
于 2009-08-19T12:19:45.290 回答
3

当仅提供相等参数时,您还可以快速修复 LambdaComparer 工作,如下所示:

    public LambdaComparer(Func<T, T, bool> lambdaComparer) :
        this(lambdaComparer, o => 1)
    {
    }
于 2011-08-23T17:38:28.680 回答
1

看这里,如何以 linq.Except 及其他方式使用和实现 IEqualityComparer。

https://www.dreamincode.net/forums/topic/352582-linq-by-example-3-methods-using-iequalitycomparer/

public class Department {
public string Code { get; set; }
public string Name { get; set; }

}

公共类 DepartmentComparer : IEqualityComparer {

// equal if their Codes are equal
public bool Equals(Department x, Department y) {
    // reference the same objects?
    if (Object.ReferenceEquals(x, y)) return true;

    // is either null?
    if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
        return false;

    return x.Code == y.Code;
}

public int GetHashCode(Department dept) {
    // If Equals() returns true for a pair of objects 
    // then GetHashCode() must return the same value for these objects.

    // if null default to 0
    if (Object.ReferenceEquals(dept, null)) return 0;

    return dept.Code.GetHashCode();
}

}

IEnumerable<Department> deptExcept = departments.Except(departments2, 
    new DepartmentComparer());

foreach (Department dept in deptExcept) {
    Console.WriteLine("{0} {1}", dept.Code, dept.Name);
}
// departments not in departments2: AC, Accounts.
于 2020-01-03T14:14:52.863 回答
0

IMO,与此问题的其他解决方案相比,上面的这个答案是最简单的解决方案。我对其进行了调整,以便对 Object 类的 Equals() 和 GetHasCode() 使用相同的逻辑。好处是这个解决方案对客户端 linq 表达式是完全透明的。

public class Ericsson4GCell
{
    public string CellName { get; set; }
    public string OtherDependantProperty { get; set; }

    public override bool Equals(Object y)
    {
        var rhsCell = y as Ericsson4GCell;
        // reference the same objects?
        if (Object.ReferenceEquals(this, rhsCell)) return true;

        // is either null?
        if (Object.ReferenceEquals(this, null) || Object.ReferenceEquals(rhsCell, null))
            return false;

        return this.CellName == rhsCell.CellName;
    }
    public override int GetHashCode()
    {
        // If Equals() returns true for a pair of objects 
        // then GetHashCode() must return the same value for these objects.

        // if null default to 0
        if (Object.ReferenceEquals(this, null)) return 0;

        return this.CellName.GetHashCode();
    }    
}
于 2021-10-08T12:34:42.247 回答