我有两个列表,我想比较更新/修改列。 比较同一类的2个列表,并在新列表中显示不同的值 我想用linq做这件事。唯一的问题是我正在处理很多列,每列超过30列。任何建议都会有很大的帮助......
//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);
答案 0 :(得分:1)
我们假设:
PartnerAndPartnerPositionEntity
类包含名为Id
的属性,表示项的唯一键鉴于上述情况,您可以:
获取您所有类型的属性
var properties = typeof(PartnerAndPartnerPositionEntity).GetProperties();
加入Id
属性上的两个列表,并遍历属性以查看哪个已更改:
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);
}
});
答案 1 :(得分:0)
好吧,如果你想避免这种枯燥乏味的方式,你需要使用反射来动态获取类成员,然后获取每个实例的值。 有关代码,请参阅C# Reflection - Get field values from a simple class。
答案 2 :(得分:0)
有......这将产生新的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();
}
}