Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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
Linq 使用IEqualityComparer检查特定值_Linq_List_C# 4.0_Distinct_Iequalitycomparer - Fatal编程技术网

Linq 使用IEqualityComparer检查特定值

Linq 使用IEqualityComparer检查特定值,linq,list,c#-4.0,distinct,iequalitycomparer,Linq,List,C# 4.0,Distinct,Iequalitycomparer,这是我第一次尝试使用IEqualityComparer,我遇到了一个问题 很可能我只是不明白代码在幕后到底在做什么 我提供的列表如下所示: Test Run | SN | Retest 1 185 0 2 185 1 3 185 1 4 185 1 public class UniqueRetests : IEqualityComparer<TestRunRecord> {

这是我第一次尝试使用IEqualityComparer,我遇到了一个问题

很可能我只是不明白代码在幕后到底在做什么

我提供的列表如下所示:

Test Run | SN    | Retest
1          185     0
2          185     1
3          185     1
4          185     1
public class UniqueRetests : IEqualityComparer<TestRunRecord>
{
    // Records are equal if their SNs and retest are equal.
    public bool Equals(TestRunRecord x, TestRunRecord y)
    {
        //Check whether any of the compared objects is null.
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        //Check whether it's a retest AND if the specified records' properties are equal.
        return x.retest == 1 && y.retest == 1 && x.boardSN == y.boardSN;
    }

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

    public int GetHashCode(TestRunRecord record)
    {
        //Check whether the object is null
        if (Object.ReferenceEquals(record, null)) return 0;

        //Get hash code for the board SN field.
        int hashRecordSN = record.boardSN.GetHashCode();

        //Get hash code for the retest field.
        int hashRecordRetest = record.retest.GetHashCode();

        //Calculate the hash code for the product.
        return hashRecordSN ^ hashRecordRetest;
    }
}
我试图使用Distinct()查找具有唯一序列号且“retest==1”的项数

var result = testRunList.Distinct(new UniqueRetests());
派生的IEqualityCompare类如下所示:

Test Run | SN    | Retest
1          185     0
2          185     1
3          185     1
4          185     1
public class UniqueRetests : IEqualityComparer<TestRunRecord>
{
    // Records are equal if their SNs and retest are equal.
    public bool Equals(TestRunRecord x, TestRunRecord y)
    {
        //Check whether any of the compared objects is null.
        if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
            return false;

        //Check whether it's a retest AND if the specified records' properties are equal.
        return x.retest == 1 && y.retest == 1 && x.boardSN == y.boardSN;
    }

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

    public int GetHashCode(TestRunRecord record)
    {
        //Check whether the object is null
        if (Object.ReferenceEquals(record, null)) return 0;

        //Get hash code for the board SN field.
        int hashRecordSN = record.boardSN.GetHashCode();

        //Get hash code for the retest field.
        int hashRecordRetest = record.retest.GetHashCode();

        //Calculate the hash code for the product.
        return hashRecordSN ^ hashRecordRetest;
    }
}
公共类UniquerTests:IEqualityComparer
{
//如果SNs和重新测试相等,则记录相等。
公共bool等于(TestRunRecord x,TestRunRecord y)
{
//检查是否有任何比较对象为空。
if(Object.ReferenceEquals(x,null)| | Object.ReferenceEquals(y,null))
返回false;
//检查是否为重新测试,以及指定记录的属性是否相等。
返回x.retest==1&&y.retest==1&&x.boardSN==y.boardSN;
}
//对于一对对象,If Equals()返回true
//然后GetHashCode()必须为这些对象返回相同的值。
public int GetHashCode(TestRunRecord记录)
{
//检查对象是否为空
if(Object.ReferenceEquals(record,null))返回0;
//获取board SN字段的哈希代码。
int hashRecordSN=record.boardSN.GetHashCode();
//获取重新测试字段的哈希代码。
int hashRecordRetest=record.retest.GetHashCode();
//计算产品的哈希代码。
返回hashRecordSN^hashRecordRetest;
}
}
问题是,这似乎产生了一个包含前两项的列表,而我要寻找的是一个只包含一项的列表,其中“retest==1”

var result = testRunList.Distinct(new UniqueRetests());
知道我做错了什么吗?如何返回“retest==0”的记录

回答


如果条件为false,则对象将被视为不相等。Distinct返回不相等的行。顺便说一句,您使用这种代码违反了IEqualityComparer的合同。结果实际上是未定义的usr

对于“违反合同”,我的意思是(例如)一个retest==0的对象将与其自身不相等usr


您需要筛选出重新测试为0的项目。将.Where(x=>x.retest!=0)放在Distinct前面。

我想知道的是,当等式语句的条件要求retest==1时,为什么会返回retest==0的项。如果该条件为false,则对象被视为不相等。Distinct返回不相等的行。顺便说一句,您使用这种代码违反了IEqualityComparer的合同。结果实际上是未定义的。对于“违反约定”,我的意思是(例如)一个retest==0的对象将与它自己不相等。啊,好的。这是有道理的。看起来像是使用了一个。在一个简单的IEqualityComparer之后,它会给我提供独特的SNs来完成这项工作。谢谢你的帮助。