相关疑难解决方法(0)

Enumerable.Except 不使用我的自定义比较器

我尝试将 except 方法与自定义相等比较器一起使用,但它不起作用。

我的平等比较器:

public class BusinessObjectGuidEqualityComparer<T> : IEqualityComparer<T> where T : BusinessObject
{
    #region IEqualityComparer<T> Members

    /// <summary>
    /// Determines whether the specified objects are equal.
    /// </summary>
    /// <param name="x">The first object of type <paramref name="T"/> to compare.</param>
    /// <param name="y">The second object of type <paramref name="T"/> to compare.</param>
    /// <returns>
    /// <see langword="true"/> If the specified objects are equal; otherwise, <see langword="false"/>.
    /// </returns>
    public bool Equals(T x, T y)
    {
        return (x == null && y …
Run Code Online (Sandbox Code Playgroud)

c# linq extension-methods

4
推荐指数
1
解决办法
3890
查看次数

C#中的字典类 - 两个对象的等价

我有一个名为Class1的类我覆盖它的Equals函数现在我有一个Dictionary的实例我添加了一个名为OBJ1的Class1实例.我有另一个名为OBJ2的Class1实例.对于OBJ1.Equals(OBJ2),代码返回true.但我在字典中找不到OBJ2.

这是伪代码

Class1 OBJ1 = new Class1(x, y, z);
Class1 OBJ2 = new Class1(a, b, c);
Dictionary<Class1, int> dic1 = new Dictionary<Class1, int>();
dic1.Add(OBJ1, 3);
OBJ1.Equals(OBJ2) -------------> return true
Dictionary.ContainsKey(OBJ2) --------------> return false
Run Code Online (Sandbox Code Playgroud)

为什么会这样?任何帮助都会受到高度欢迎

c# dictionary equals

4
推荐指数
2
解决办法
4973
查看次数

从 VB.NET 中的 List(Of T) 中删除重复项

我无法从我的列表中删除重复项。我究竟做错了什么?

Dim Contacts As New List(Of Person)

...

' remove duplicates '
Contacts = Contacts.Distinct(New PersonEqualityComparer).ToList
Run Code Online (Sandbox Code Playgroud)

我的平等比较器:

Public Class PersonEqualityComparer
    Implements IEqualityComparer(Of Person)
    Public Function Equals1(ByVal x As Person, ByVal y As Person) As Boolean Implements System.Collections.Generic.IEqualityComparer(Of Person).Equals
        Return String.Equals(x.EmailAddress, y.EmailAddress, StringComparison.CurrentCultureIgnoreCase) AndAlso _
               String.Equals(x.GivenName, y.GivenName, StringComparison.CurrentCultureIgnoreCase) AndAlso _
               String.Equals(x.Surname, y.Surname, StringComparison.CurrentCultureIgnoreCase)
    End Function
    Public Function GetHashCode1(ByVal obj As Person) As Integer Implements System.Collections.Generic.IEqualityComparer(Of Person).GetHashCode
        Return obj.GetHashCode
    End Function
End Class
Run Code Online (Sandbox Code Playgroud)

.net vb.net generics

3
推荐指数
1
解决办法
6592
查看次数

过滤列表中的重复项

我有一个列表,我想过滤重复的项目.在询问这个问题之前,我在StackOverflow上搜索并找到了两个解决方案; 使用.Distinct()和使用HashSet,但这些方法都不适合我.我试图过滤的对象实现了该.Equals方法,但它仍然无效.

我通过创建500个完全相同的对象并将它们放在列表中来测试它.我预计会有1人离开,但所有500人仍在那里.我的对象是否需要实现其他方法才能工作?

谢谢.

c# list hashset

3
推荐指数
1
解决办法
79
查看次数

为什么在.NET中,哈希表和字典不使用Equals()方法而不是GetHashCode进行键比较?

在.NET中,只要我们为类重写Equals()方法,通常的做法就是也重写GetHashCode()方法。当在哈希表和字典中使用该对象时,这样做将确保更好的性能。仅当两个键的GetHashCode()值相同时,它们才视为在Hashtable中相等。我的问题是,为什么Hashtables不能使用Equals()方法比较键?那会消除重写GetHashCode()方法的负担。

.net c# hashtable equals

3
推荐指数
1
解决办法
637
查看次数

IEnumerable.Except()如何工作?

我试图排除要添加到数据库中的实体(如果它们已经存在)。因此,我认为newBillInstances.Except(dbContext.BillInstances)这是最好的方法。但是,它完全不起作用(不排除任何实体),因为List<string>它可以完美地工作。

我读了这个讨论和实际能解密.Except()MSDN。它指出.Except()要实现的类应IEqualityComparer<T>使用默认比较器。

实际上,MSDN文章并未完全描述两个实例的比较过程。我仍然不明白为什么Equals()和GetHashObject()都必须被覆盖。

我已经实现了IEqualityComparer<BillInstance>接口,并在两种方法中都设置了断点,但是在调用.Except(IEnumerable)它时并未使用。只有当我更改为时,我才会.Except(IEnumerable, new BillInstanceComparer())咳嗽,GetHashCode()但咳嗽不会中断Equals()

然后,我IEqualityComparer<BillInstance>BillInstance类中实现了正确的方法,并期望在使用时可以使用它,.Except(IEnumerable)但是两种方法都不会造成中断。

所以我有两个问题:

  1. 应该做什么使用.Except(IEnumerable)
  2. 为什么Equals()根本不使用?是否仅在两个实例的哈希码相同的情况下使用?

c# linq ienumerable

3
推荐指数
1
解决办法
828
查看次数

collections.Contains(T)方法

我正在使用a System.Collections.Generic,其中包含我编写的类的实例.

我已经读过collections .Contains方法使用object.Equals(),或者Equals()IEquatable接口中实现该方法.

我已经覆盖了对象方法,以及从界面实现.但是,Queue.Contains(instance)总是返回false.我究竟做错了什么?

例如...

class Foo : IEquatable<Foo>
{
    ...
    int fooField1;
    ...

    public override bool Equals(object obj)
    {
         Foo other = obj as Foo;
         bool isEqual = false;

         if (other.fooField1 == this.fooField1)
         { 
             isEqual = true;
         }

         return isEqual;         
    }

    public bool Equals(Foo other)
    {
         bool isEqual = false;

         if (other.fooField1 == this.fooField1)
         { 
             isEqual = true;
         }

         return isEqual;         
    }

}
...

void SomeFunction()
{
    Queue<Foo> …
Run Code Online (Sandbox Code Playgroud)

c# containers contains equals iequatable

2
推荐指数
1
解决办法
2720
查看次数

SequenceEqual为true,但HashSet.SetEquals为false

我一直在执行设定平等(即列表比较,其中的顺序是无关紧要的)和阅读做题等之后尝试这个这个,写了下面的简单扩展:

    public static bool SetEqual<T>(this IEnumerable<T> enumerable, IEnumerable<T> other)
    {
        if (enumerable == null && other == null)
            return true;

        if (enumerable == null || other == null)
            return false;

        var setA = new HashSet<T>(enumerable);
        return setA.SetEquals(other);
    }
Run Code Online (Sandbox Code Playgroud)

但是,我遇到了一个简单的数据结构,这种方法不起作用,而Enumerable.SequenceEqual则不行.

    public class Test : IEquatable<Test>
    {
        public Guid Id { get; set; }
        public List<Test> RelatedTest { get; set; }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if …
Run Code Online (Sandbox Code Playgroud)

c# linq equality set sequence

2
推荐指数
1
解决办法
1759
查看次数

避免在数据库中添加重复项的最佳方法

我有一个包含三列的SQL Server表:

表格1

col1 int
col2 int
col3 string
Run Code Online (Sandbox Code Playgroud)

我为所有三列定义了一个唯一约束 (col1, col2, col3)

现在,我有一个.csv文件,我想在此表中添加记录,*.csv文件可以有重复记录.

我在上面的场景中搜索了各种避免重复的选项.以下是适合我的三个选项.请看一下并提出一些关于每种方法的优点/缺点的想法,以便我可以选择最好的方法.

选项1 :

首先避免重复,即从csv文件向列表添加对象时.我用过HashSet<T>了这个并覆盖了类型T下面的方法:

public override int GetHashCode()
{
    return col1.GetHashCode() + col2.GetHashCode() + col3.GetHashCode();
}

public override bool Equals(object obj)
{
    var other = obj as T;
    if (other == null)
    {
        return false;
    }
    return col1 == other.col1
        && col2 == other.col2
        && col3 == other.col3;
}
Run Code Online (Sandbox Code Playgroud)

选项#2

List<T>代替HashSet<T>.

添加所有对象后删除重复项 List<T>

    List<T> distinctObjects = allObjects
        .GroupBy(x => new …
Run Code Online (Sandbox Code Playgroud)

c# sql-server hashset

2
推荐指数
1
解决办法
1684
查看次数

IEquatable不会调用Equals方法

嗯,我正面临IEquatable(C#)的问题.正如您在下面的代码中看到的,我得到了一个我实现IEquatable的类,但它的"Equals"方法无法实现.我的目标是:我的数据库中有一个日期时间列,我想区分日期,而不是考虑"时间"部分.

例如:12-01-2014 23:14将等于12-01-2014 18:00.

namespace MyNamespace
{
    public class MyRepository
    {
        public void MyMethod(int id)
        {
            var x = (from t in context.MyTable
                     where t.id == id
                     select new MyClassDatetime()
                     {
                         Dates = v.Date
                     }).Distinct().ToList();
        }
    }


public class MyClassDatetime : IEquatable<MyClassDatetime>
{
    public DateTime? Dates { get; set; }

    public bool Equals(MyClassDatetime other)
    {
        if (other == null) return false;
        return (this.Dates.HasValue ? this.Dates.Value.ToShortDateString().Equals(other.Dates.Value.ToShortDateString()) : false);
    }

    public override bool Equals(object other)
    {
        return this.Equals(other as MyClassDatetime );
    } …
Run Code Online (Sandbox Code Playgroud)

c# datetime entity-framework distinct iequatable

2
推荐指数
1
解决办法
460
查看次数