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_Distinct - Fatal编程技术网

C# 使用LINQ选择唯一的项目

C# 使用LINQ选择唯一的项目,c#,linq,distinct,C#,Linq,Distinct,当我使用下面的代码时,我多次得到相同的项目 XElement neededFiles = new XElement("needed", from o in _9nFiles.Elements() join t in addedToSitePull.Elements() on o.Value equals t.Value where o.Value == t.Value select new XElement("pic", o.V

当我使用下面的代码时,我多次得到相同的项目

XElement neededFiles = new XElement("needed",
    from o in _9nFiles.Elements()
    join t in addedToSitePull.Elements()
         on o.Value equals
         t.Value
    where o.Value == t.Value
    select new XElement("pic", o.Value));
我只想要独一无二的东西。我看到一个使用它的堆栈溢出帖子,我试图实现它,但更改没有影响

守则:

XElement neededFiles = new XElement("needed",
(from o in _9nFiles.Elements()
join t in addedToSitePull.Elements()
on o.Value equals
 t.Value
 where o.Value == t.Value
select new XElement("pic", o.Value)).Distinct() );

我想这不起作用的原因是因为
XElement.Equals
使用了一个简单的引用相等检查,而不是比较两个项的
属性。如果要比较这些值,可以将其更改为:

_9nfiles.Elements()
    .Join(addedToSitePull, o => o.Value, t => t.Value, (o, t) => o.Value)
    .Distinct()
    .Select(val => new XElement("pic", val));
您还可以创建自己的
IEqualityComparer
,用于比较两个
XElement
s的值。注意:假设所有值均为非空:

public class XElementValueEqualityComparer : IEqualityComparer<XElement>
{
    public bool Equals(XElement x, XElement y)
    {
        return x.Value.Equals(y.Value);
    }

    public int GetHashCode(XElement x)
    {
        return x.Value.GetHashCode();
    }
}
公共类XElementValueEqualityComparer:IEqualityComparer
{
公共布尔等于(像素x,像素y)
{
返回x.Value等于(y.Value);
}
公共整数GetHashCode(XElement x)
{
返回x.Value.GetHashCode();
}
}

然后您可以使用
Distinct(新的XElementValueEqualityComparer())替换对
Distinct
的现有调用Distinct不起作用,因为XElements是通过引用而不是通过值进行比较的。 解决方案是使用另一个Distinct-Distinct(IEqualityComparer)重载

例如,您需要实现IEqualityComparer,如下所示:

class XElementEqualityComparer : IEqualityComparer<XElement>
    {
        #region IEqualityComparer<XElement> Members

        public bool Equals(XElement x, XElement y)
        {
            if (x == null ^ y == null)
                return false;

            if (x == null && y == null)
                return true;

            return x.Value == y.Value;
        }

        public int GetHashCode(XElement obj)
        {
            if (obj == null)
                return 0;

            return obj.Value.GetHashCode();
        }

        #endregion
    }
类XElementEqualityComparer:IEqualityComparer
{
#地区资格比较成员
公共布尔等于(像素x,像素y)
{
如果(x==null^y==null)
返回false;
如果(x==null&&y==null)
返回true;
返回x.值==y.值;
}
公共整数GetHashCode(XElement obj)
{
if(obj==null)
返回0;
返回obj.Value.GetHashCode();
}
#端区
}

这不是一个好的解决方案,但非常简单

foreach (XElement  pic in neededFiles.Elements())
{
    unSyncedPictures.Add(pic.Value);
}
List<string> temp = new List<string>();
temp.AddRange(unSyncedPictures.Distinct());
foreach(neededFiles.Elements()中的XElement pic)
{
未同步图片。添加(图片值);
}
列表温度=新列表();
temp.AddRange(unSyncedPictures.Distinct());

Lee的答案更好,因为它更紧凑。我只留下我的答案,例如+1感谢您的回答+使用其他解决方案的罕见建议。+1尽管由于时间问题,我不得不实施一个糟糕的解决方案,但我期待着测试和学习您的解决方案-尽快谢谢