Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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#Linq,但不返回不同值的列表_C#_Linq - Fatal编程技术网

c#Linq,但不返回不同值的列表

c#Linq,但不返回不同值的列表,c#,linq,C#,Linq,我试图找出两个列表中的差异。与列表“x”相比,列表“y”应具有1个唯一值。但是,Exception不会返回差异。“差异”列表的计数始终等于0 List<EtaNotificationUser> etaNotifications = GetAllNotificationsByCompanyIDAndUserID(PrevSelectedCompany.cmp_ID); IEnumerable<string> x = etaNotifications.OfType<st

我试图找出两个列表中的差异。与列表“x”相比,列表“y”应具有1个唯一值。但是,Exception不会返回差异。“差异”列表的计数始终等于0

List<EtaNotificationUser> etaNotifications = GetAllNotificationsByCompanyIDAndUserID(PrevSelectedCompany.cmp_ID);
IEnumerable<string> x = etaNotifications.OfType<string>();
IEnumerable<string> y = EmailList.OfType<string>();

IEnumerable<string> differences = x.Except(y,  new StringLengthEqualityComparer()).ToList();

   foreach(string diff in differences)
                {
                    addDiffs.Add(diff);
                }
List etaNotifications=GetAllNotificationsByCompanyIDAndUserID(PrevSelectedCompany.cmp\u ID);
IEnumerable x=etaNotifications.OfType();
IEnumerable y=EmailList.OfType();
IEnumerable differences=x.Except(y,新字符串长度质量比较程序()).ToList();
foreach(字符串差异)
{
添加差异。添加(差异);
}
在阅读了几篇文章后,我创建了一个自定义比较器。比较器查看字符串长度(保持简单以便测试)并获得哈希代码,因为这是两个不同类型的对象(即使我将它们的类型转换为字符串),我认为这可能是问题所在

class StringLengthEqualityComparer : IEqualityComparer<string>
{
    public bool Equals(string x, string y)
    {
        return x.Length == y.Length;
    }

    public int GetHashCode(string obj)
    {
        return obj.Length;
    }
}
类字符串长度QualityComparer:IEqualityComparer
{
公共布尔等于(字符串x、字符串y)
{
返回x.Length==y.Length;
}
public int GetHashCode(字符串obj)
{
返回对象长度;
}
}
这是我第一次使用除之外的
。听起来这是比较两个列表的一种很好的优化方法,但我无法让它发挥作用

更新 X-应该保存数据库中的电子邮件地址。 GetAllNotificationsByCompanyIDAndUserID-从数据库返回电子邮件值。 Y-应保存UI网格中的所有电子邮件地址

我试图做的是检测是否已将新电子邮件添加到网格中。因此,在这一点上,X将保存来自过去条目的值。Y将有用户添加的任何新电子邮件地址,但尚未保存

我已经验证了这一切是否正常工作。

问题在于:

IEnumerable<string> x = etaNotifications.OfType<string>();
如果
EtaNotificationUser
已覆盖
ToString
,以提供要比较的值。如果要比较的值位于属性中,则可以使用:

IEnumerable<string> x = etaNotifications.Select(e => e.EmailAddress);
IEnumerable x=etaNotifications.Select(e=>e.EmailAddress);
或者其他一些财产

您可能需要为
y
执行类似操作(除非
EmailList
已经是
列表,我对此表示怀疑)。

问题在于:

IEnumerable<string> x = etaNotifications.OfType<string>();
如果
EtaNotificationUser
已覆盖
ToString
,以提供要比较的值。如果要比较的值位于属性中,则可以使用:

IEnumerable<string> x = etaNotifications.Select(e => e.EmailAddress);
IEnumerable x=etaNotifications.Select(e=>e.EmailAddress);
或者其他一些财产


您可能需要对
y
执行类似的操作(除非
EmailList
已经是
列表,我对此表示怀疑)。

假设您已经验证了两个可枚举项x和y实际上包含了您希望它们包含的字符串,我相信您的问题在于字符串比较器。根据文档,“生成两个序列的集差。集差是第一个序列中没有出现在第二个序列中的成员。”但相等比较器会将所有长度相同的字符串相等。因此,如果第一个序列中的字符串恰好与第二个序列中的字符串具有相同的长度,则使用比较器不会发现它的不同

更新:是的,我刚刚测试过:

public class StringLengthEqualityComparer : IEqualityComparer<string>
{
    public bool Equals(string x, string y)
    {
        return x.Length == y.Length;
    }

    public int GetHashCode(string obj)
    {
        return obj.Length;
    }
}


        string [] array1 = new string [] { "foo", "bar", "yup" };
        string[] array2 = new string[] { "dll" };
        int diffCount;

        diffCount = 0;
        foreach (var diff in array1.Except(array2, new StringLengthEqualityComparer()))
        {
            diffCount++;
        }

        Debug.Assert(diffCount == 0); // No assert.

        diffCount = 0;
        foreach (var diff in array1.Except(array2))
        {
            diffCount++;
        }

        Debug.Assert(diffCount == 0); // Assert b/c diffCount == 3.
公共类字符串长度QualityComparer:IEqualityComparer
{
公共布尔等于(字符串x、字符串y)
{
返回x.Length==y.Length;
}
public int GetHashCode(字符串obj)
{
返回对象长度;
}
}
字符串[]数组1=新字符串[]{“foo”,“bar”,“yup”};
字符串[]数组2=新字符串[]{“dll”};
内扩散;
扩散计数=0;
foreach(数组1中的变量差异。除了(数组2,新字符串长度QualityComparer()))
{
扩散计数++;
}
Assert(diffCount==0);//没有断言。
扩散计数=0;
foreach(阵列1中的var差异。除了(阵列2))
{
扩散计数++;
}
Assert(diffCount==0);//断言b/c diffCount==3。

自定义比较器没有断言,但标准比较器有断言。

假设您已经验证了两个可枚举的x和y实际上包含您希望它们包含的字符串,我相信您的问题在于字符串比较器。根据文档,“生成两个序列的集差。集差是第一个序列中没有出现在第二个序列中的成员。”但相等比较器会将所有长度相同的字符串相等。因此,如果第一个序列中的字符串恰好与第二个序列中的字符串具有相同的长度,则使用比较器不会发现它的不同

更新:是的,我刚刚测试过:

public class StringLengthEqualityComparer : IEqualityComparer<string>
{
    public bool Equals(string x, string y)
    {
        return x.Length == y.Length;
    }

    public int GetHashCode(string obj)
    {
        return obj.Length;
    }
}


        string [] array1 = new string [] { "foo", "bar", "yup" };
        string[] array2 = new string[] { "dll" };
        int diffCount;

        diffCount = 0;
        foreach (var diff in array1.Except(array2, new StringLengthEqualityComparer()))
        {
            diffCount++;
        }

        Debug.Assert(diffCount == 0); // No assert.

        diffCount = 0;
        foreach (var diff in array1.Except(array2))
        {
            diffCount++;
        }

        Debug.Assert(diffCount == 0); // Assert b/c diffCount == 3.
公共类字符串长度QualityComparer:IEqualityComparer
{
公共布尔等于(字符串x、字符串y)
{
返回x.Length==y.Length;
}
public int GetHashCode(字符串obj)
{
返回对象长度;
}
}
字符串[]数组1=新字符串[]{“foo”,“bar”,“yup”};
字符串[]数组2=新字符串[]{“dll”};
内扩散;
扩散计数=0;
foreach(数组1中的变量差异。除了(数组2,新字符串长度QualityComparer()))
{
扩散计数++;
}
Assert(diffCount==0);//没有断言。
扩散计数=0;
foreach(阵列1中的var差异。除了(阵列2))
{
扩散计数++;
}
Assert(diffCount==0);//断言b/c diffCount==3。

自定义比较器没有断言,但标准比较器有断言。

请显示一个简短但完整的程序来演示问题。我们不知道
x
y
中有什么。您的相等比较器看起来很好。请验证通知。ofType源列表项。请显示一个简短但完整的程序来演示此问题。