Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/307.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# Exception()-方法与CustomPropertyQualityComparer一起使用,并且没有明显的_C#_Linq_Intersect_Except - Fatal编程技术网

C# Exception()-方法与CustomPropertyQualityComparer一起使用,并且没有明显的

C# Exception()-方法与CustomPropertyQualityComparer一起使用,并且没有明显的,c#,linq,intersect,except,C#,Linq,Intersect,Except,我使用自己的CustomPropertyQualityComparer按自定义属性动态比较对象列表 这是CustomPropertyQualityComparer: public class CustomPropertyEqualityComparer<T> : IEqualityComparer<T> where T : class { public PropertyInfo[] _PropertyInfos; /// <summary>

我使用自己的CustomPropertyQualityComparer按自定义属性动态比较对象列表

这是CustomPropertyQualityComparer:

public class CustomPropertyEqualityComparer<T> : IEqualityComparer<T> where T : class
{
    public PropertyInfo[] _PropertyInfos;

    /// <summary>
    /// Creates a new instance of PropertyComparer.
    /// </summary>
    /// <param name="propertyName">The name of the property on type T 
    /// to perform the comparison on.</param>
    public CustomPropertyEqualityComparer(params string[] propertyName)
    {
        _PropertyInfos = new PropertyInfo[propertyName.Length];

        //store a reference to the property info object for use during the comparison
        for (int i = 0; i < propertyName.Length; i++)
        {
            _PropertyInfos[i] = typeof(T).GetProperty(propertyName[i], BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
            if (_PropertyInfos == null)
            {
                throw new ArgumentException(string.Format("{0} is not a property of type {1}.", propertyName, typeof(T)));
            }
        }
    }

    public bool Equals(T x, T y)
    {
        foreach (PropertyInfo propInf in _PropertyInfos)
        {
            if (propInf != null)
            {
                //get the current value of the comparison property of x and of y
                object xValue = propInf.GetValue(x, null);
                object yValue = propInf.GetValue(y, null);

                //if the xValue is null then we consider them equal if and only if yValue is null
                if (xValue != yValue && (xValue == null || !xValue.Equals(yValue)))
                {
                    return false;
                }    
            }               

        }
        return true;
    }

    public int GetHashCode(T obj)
    {
        int HashCode = 0;

        foreach (var p in _PropertyInfos)
        {
            if (p != null)
            {
                HashCode += p.GetHashCode();    
            }                
        }

        return HashCode;
    }
}
公共类CustomPropertyQualityComparer:IEqualityComparer其中T:class
{
公共财产信息[]和财产信息;
/// 
///创建PropertyComparer的新实例。
/// 
///类型T上的属性的名称
///在上执行比较。
公共CustomPropertyQualityComparer(参数字符串[]propertyName)
{
_PropertyInfo=新的PropertyInfo[propertyName.Length];
//存储对属性信息对象的引用,以便在比较期间使用
for(int i=0;i
我是这样使用的:

List<T> ListNewObj = ListObj2.Except(ListObj1, CustomPropertyComparer).ToList();
List ListNewObj=ListObj2.Except(ListObj1,CustomPropertyComparer).ToList();
假设ListObj2有两个相同的项,ListObj1为空,ListNewObj只包含ListObj2中的一个项,但我希望它们都在ListNewObj中

问题是,在Except()和Intersect()之后,似乎会自动出现Distinct(),但如何避免这种情况

问题是,似乎在Except()和Intersect()之后会自动出现Distinct()

不是真的。这只是意味着它是一个基于集合的操作-因此结果永远不会包含两个相等的(在相等比较下)结果。如果你看看我的电脑,你会发现它不需要两步操作。不幸的是,它并没有很清楚的记录

避免这种情况的最简单方法就是自己执行过滤:

var excludedSet = new HashSet<T>(ListObj1, CustomPropertyComparer);
var result = ListObj2.Where(x => !excludedSet.Contains(x))
                     .ToList();
var excludedSet=新哈希集(ListObj1,CustomPropertyComparer);
var result=ListObj2.Where(x=>!excludedSet.Contains(x))
.ToList();