繁体   English   中英

自定义 IEqualityComparer 使用 LINQ 计算两个列表之间的差异,除了

[英]Custom IEqualityComparer to calculate Difference between two lists using LINQ Except

我使用的这个回答两个列表之间的差异发现使用LINQ两个列表之间的差异Except 但是,我在列表中的对象是泛型类型,所以我为泛型类型编写了一个比较器,然后将它传递给了 except 方法。 但是我收到以下错误消息:

错误 CS1929 “List”不包含“Except”的定义,并且最佳扩展方法重载“ParallelEnumerable.Except(ParallelQuery, ParallelQuery, IEqualityComparer)”需要“ParallelQuery”类型的接收器

我的电话:

var differenceList = list1.Except(list2, new PropertyComparer<Guid>("ObjectId"));

还有我的比较器:

public class PropertyComparer<T> : IEqualityComparer<T>
{
    private PropertyInfo _PropertyInfo;
    public PropertyComparer(string propertyName)
    {
        _PropertyInfo = typeof(T).GetProperty(propertyName, 
            BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
        if (_PropertyInfo == null)
        {
            throw new ArgumentException(
                string.Format("{0} is not a property of type {1}.", propertyName, typeof(T)));
        }
    }

    public int GetHashCode(T obj)
    {
        object propertyValue = _PropertyInfo.GetValue(obj, null);
        if (obj == null) return 0;
        return propertyValue.GetHashCode();
    }

    public bool Equals(T x, T y)
    {
        object xValue = _PropertyInfo.GetValue(x, null);
        object yValue = _PropertyInfo.GetValue(y, null);

        if (xValue == null)
        {
            return yValue == null;
        }

        return xValue.Equals(yValue);
    }
}

System.Linq方法声明,它适合我的比较器:

public static IEnumerable<TSource> Except<TSource>(
    this IEnumerable<TSource> first, 
    IEnumerable<TSource> second, 
    IEqualityComparer<TSource> comparer
);

我做错了什么,我怎样才能让它工作?


更新

list1list2类型:

List<Contact>

Contact在哪里

public class Contact : EntityBase
{
    public Guid ContactId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public List<Email> Emails {get; set;}
}

EntityBase

public class EntityBase : IEntity
{
    public IQueryable<T> GetDBEntities<T>(ApplicationDbContext db) where T : class
    {
        return db.Set<T>();
    }

    public List<T> GetLocalEntities<T>() where T : class
    {
        var localProperties = this.GetType().GetProperties();
        foreach (var localProperty in localProperties)
        {
            var localPropertyValue = localProperty.GetValue(this);
            if (localPropertyValue != null && localPropertyValue.IsGenericList() == true)
            {
                var localPropertyValueType = localPropertyValue.GetType(); // List<object>
                var localPropertyValueTypeDecoupled = localPropertyValueType.GetGenericArguments().Single(); // List<T>
                if (localPropertyValueTypeDecoupled == typeof(T))
                {
                    return (List<T>)localPropertyValue;
                }
            }
        }
        throw new Exception("Entity Types Validation Error");
    }

    public void ProcessEntityReference<T>(ApplicationDbContext db) where T : class
    {
        // T is Email
        var remoteList = this.GetDBEntities<T>(db).ToList();
        var updatedList = GetLocalEntities<T>();
        var toBeAdded = updatedList.Except(remoteList, new PropertyComparer<Guid>("ContactId"));
        var toBeDeleted = new List<object>();
        throw new NotImplementedException();
    }

    public void ProcessEntityReferences(ApplicationDbContext db)
    {
        this.ProcessEntityReference<Email>(db);
    }
}

您应该将列表使用的相同类型传递给 except 方法,在您的示例中,您使用的是Guid但它应该是Contact类型,此外,您的Contact类没有名为“ObjectId”的属性,请尝试更改它对于“ ContactId ”,以下似乎工作正常:

static void Main(string[] args)
{
     var list1 = new List<Contact>();
     list1.Add(new Contact() { ContactId = Guid.Parse("FB58F102-0CE4-4914-ABFF-ABBD3895D719") });
     list1.Add(new Contact() { ContactId = Guid.Parse("5A201238-6036-4385-B848-DEE598A3520C") });

     var list2 = new List<Contact>();
     list2.Add(new Contact() { ContactId = Guid.Parse("FB58F102-0CE4-4914-ABFF-ABBD3895D719") });

     var list3 = list1.Except(list2, new PropertyComparer<Contact>("ContactId"));

     foreach (var item in list3) 
         Console.WriteLine(item.ContactId.ToString());

     Console.ReadLine();
}

public class Contact
{
    public Guid ContactId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class PropertyComparer<T> : IEqualityComparer<T>
{
    private PropertyInfo _PropertyInfo;
    public PropertyComparer(string propertyName)
    {
        _PropertyInfo = typeof(T).GetProperty(propertyName,
            BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
        if (_PropertyInfo == null)
        {
            throw new ArgumentException(
                string.Format("{0} is not a property of type {1}.", propertyName, typeof(T)));
        }
    }

    public int GetHashCode(T obj)
    {
        object propertyValue = _PropertyInfo.GetValue(obj, null);
        if (obj == null) return 0;
        return propertyValue.GetHashCode();
    }

    public bool Equals(T x, T y)
    {
        object xValue = _PropertyInfo.GetValue(x, null);
        object yValue = _PropertyInfo.GetValue(y, null);

        if (xValue == null)
        {
            return yValue == null;
        }

        return xValue.Equals(yValue);
    }
}

输出:

5a201238-6036-4385-b848-dee598a3520c

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM