简体   繁体   中英

Compare Lists of same class

I have two lists I would like to compare them for updated/modified columns. Compare 2 Lists of the same class and show the different values in a new list I would like to do this using linq. The only problem is I am dealing with a lot of columns, over excess of 30 columns in each. Any suggestions would be of great help...

        //In Dal
        List<PartnerAndPartnerPositionEntity> GetAllPartnerAndPartnerPositionOldDB(int modelId);
        List<PartnerAndPartnerPositionEntity> GetAllPartnerAndPartnerPosition(int modelId);
        //BL
        //get from new db
        var list1= _partnerDAL.GetAllPartnerAndPartnerPosition(modelId);
        //get from old db
        var list2= _partnerDAL.GetAllPartnerAndPartnerPositionOldDB(modelId);

Let's assume that:

  1. PartnerAndPartnerPositionEntity class contains a property named Id that represents the unique key of an item

Given the above you can:

Get all properties of your type

var properties = typeof(PartnerAndPartnerPositionEntity).GetProperties();

Join the two lists on the Id property and iterate through the properties to see which one has changed:

var list = list1.Join(list2,
        x => x.Id,
        y => y.Id,
        (x, y) => Tuple.Create(x, y))
    .ToList();

list.Foreach(tuple =>
{
    foreach(var propertyInfo in properties)
    {
        var value1 = propertyInfo.GetValue(tuple.Item1, null);
        var value2 = propertyInfo.GetValue(tuple.Item2, null);
        if(value1 != value2)
            Console.WriteLine("Item with id {0} has different values for property {1}.",
                tuple.Item1,Id, propertyInfo.Name);
    }
});

Well, if you want to avoid doing it the boring and tedious way, you need to use reflection to dynamically get the class members then get their values for each instance. See C# Reflection - Get field values from a simple class for the code.

There...this will generate new IL!

public static class EqualityHelper
{
    private ConcurrentDictionary<Type, object> _cache = new ConcurrentDictionary<Type, object>();
    public bool AreEqual<T>(T left, T right)
    {
        var equality = (Func<T,T,bool>)_cache.GetOrAdd(typeof(T), CreateEquality<T>());
        return equality(left, right);
    }

    private Func<T, T, bool> CreateEquality<T>()
    {
        var left = Expression.Parameter(typeof(T));
        var right = Expression.Parameter(typeof(T));
        var properties = from x in typeof(T).GetProperties()
                        where x.GetIndexParameters().Any() == false
                        select x;
        var expressions = from p in properties
                        select Expression.Equal(
                            Expression.Property(left, p),
                            Expression.Property(right, p));
        var body = expressions.Aggregate(Expression.AndAlso);
        var lambda = Expression.Lambda<Func<T,T,bool>>(body, new [] {left, right});
        return lambda.Compile();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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