Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用HashSet C选择项目#_C#_Collections_Hashset - Fatal编程技术网

C# 使用HashSet C选择项目#

C# 使用HashSet C选择项目#,c#,collections,hashset,C#,Collections,Hashset,我有一个哈希集。是否有一种方法可以利用IEqualityComparer检索您传入的对象中的项,该对象将满足IEqualityComparer中定义的equals方法 这也许可以解释得更清楚一点 public class Program { public static void Main() { HashSet<Class1> set = new HashSet<Class1>(new Class1Comparer());

我有一个哈希集。是否有一种方法可以利用IEqualityComparer检索您传入的对象中的项,该对象将满足IEqualityComparer中定义的equals方法

这也许可以解释得更清楚一点

    public class Program
{
    public static void Main()
    {
        HashSet<Class1> set = new HashSet<Class1>(new Class1Comparer());
        set.Add( new Class1() { MyProperty1PK = 1, MyProperty2 = 1});
        set.Add( new Class1() { MyProperty1PK = 2, MyProperty2 = 2});

        if (set.Contains(new Class1() { MyProperty1PK = 1 }))
            Console.WriteLine("Contains the object");

        //is there a better way of doing this, using the comparer?  
        //      it clearly needs to use the comparer to determine if it's in the hash set.
        Class1 variable = set.Where(e => e.MyProperty1PK == 1).FirstOrDefault();

        if(variable != null)
            Console.WriteLine("Contains the object");
    }
}

class Class1
{
    public int MyProperty1PK { get; set; }
    public int MyProperty2 { get; set; }
}

class Class1Comparer : IEqualityComparer<Class1>
{
    public bool Equals(Class1 x, Class1 y)
    {
        return x.MyProperty1PK == y.MyProperty1PK;
    }

    public int GetHashCode(Class1 obj)
    {
        return obj.MyProperty1PK;
    }
}
公共类程序
{
公共静态void Main()
{
HashSet set=newhashset(new classascomparer());
Add(new Class1(){MyProperty1PK=1,MyProperty2=1});
Add(new Class1(){MyProperty1PK=2,MyProperty2=2});
if(set.Contains(new Class1(){MyProperty1PK=1}))
Console.WriteLine(“包含对象”);
//有没有更好的方法,使用比较器?
//它显然需要使用比较器来确定它是否在哈希集中。
Class1变量=set.Where(e=>e.MyProperty1PK==1.FirstOrDefault();
if(变量!=null)
Console.WriteLine(“包含对象”);
}
}
一班
{
公共int MyProperty1PK{get;set;}
公共int MyProperty2{get;set;}
}
类ClassComparer:IEqualityComparer
{
公共布尔等于(1类x,1类y)
{
返回x.MyProperty1PK==y.MyProperty1PK;
}
公共整数GetHashCode(Class1 obj)
{
返回obj.MyProperty1PK;
}
}
如果要基于单个属性检索项目,可能需要使用
字典而不是哈希集。然后,您可以使用
MyProperty1PK
作为键,将项目放入字典中

然后,您的查询变得简单:

Class1 variable;
if (!dictionary.TryGetValue(1, out variable)
{
  // class wasn't in dictionary
}

假设您已经在使用仅使用此值作为唯一性标准的比较器进行存储,那么仅使用该属性作为字典中的键并不存在任何缺点。

您的GetHashCode可能会返回属性的哈希代码,而不是属性itself@pstrjds没错——不过在这种情况下(因为它是一个int),这仍然有效。@ReedCopsey-我更多地是从“最佳实践”的角度来研究它。接下来我将研究实现IEqualityComparer的最佳实践。:-)我同意,但有一个值字典…存储在键中,然后在value属性中什么都没有存储,这看起来很奇怪。这样的事情值得考虑吗_set.Intersect(新列表{item}).FirstOrDefault()看起来不是,因为这只是IEnumerable上的一个扩展方法。。。谢谢你的帮助。@priehl我会使用
字典
-将道具存储在key中,将类本身存储在value中。。。