我创建了一个类,通过一个属性与泛型列表进行比较。
public class ListComparator<TOriginal,TNew>
{
public ListComparator(IEnumerable<TOriginal> iOriginalList, Func<TOriginal, IComparable> iOriginalProperty, IEnumerable<TNew> iNewList, Func<TNew, IComparable> iNewProperty)
{
if (iOriginalProperty == null ||
iNewProperty == null) { throw new ArgumentNullException(); };
if (iOriginalList.IsNullOrEmpty() )
{
NewList = (iNewList != null) ? iNewList.ToList() : null;
return;
}
if (iNewList.IsNullOrEmpty())
{
RemovedList = (iOriginalList != null) ? iOriginalList.ToList() : null;
return;
}
NewList = (from tnew in iNewList.ToList()
join toriginal in iOriginalList.ToList()
on iNewProperty(tnew)
equals iOriginalProperty(toriginal) into gj
from item in gj.DefaultIfEmpty()
where item == null
select tnew).ToList();
CommunList = (from tnew in iNewList.ToList()
join toriginal in iOriginalList.ToList()
on iNewProperty(tnew)
equals iOriginalProperty(toriginal) into gj
from item in gj.DefaultIfEmpty()
where item != null
select tnew).ToList();
CommunPairList = (from tnew in iNewList.ToList()
join toriginal in iOriginalList.ToList()
on iNewProperty(tnew)
equals iOriginalProperty(toriginal) into gj
from item in gj.DefaultIfEmpty()
where item != null
select new KeyValuePair<TOriginal, TNew>(item, tnew)).ToList();
RemovedList = (from toriginal in iOriginalList.ToList()
join tnew in iNewList.ToList()
on iOriginalProperty(toriginal)
equals iNewProperty(tnew) into gj
from item in gj.DefaultIfEmpty()
where item == null
select toriginal).ToList();
return;
}
#region PROPRIETES
public List<TNew> NewList { get; private set; }
public List<TOriginal> RemovedList { get; private set; }
public List<TNew> CommunList { get; private set; }
/// <summary>
/// Obtient la liste de pair avec l'original en key et le nouveau en value
/// </summary>
public List<KeyValuePair<TOriginal,TNew>> CommunPairList { get; private set; }
#endregion
我这样用:
List<Tuple<string, string>> list1 = new List<Tuple<string, string>>();
List<Tuple<string, string>> list2 = new List<Tuple<string, string>>();
list1.Add(new Tuple<string, string>("AA", "zefzef"));
list1.Add(new Tuple<string, string>("A1", "iulyu"));
list2.Add(new Tuple<string, string>("Abb", "szefez"));
list2.Add(new Tuple<string, string>("A1", "zevzez"));
ListComparator<Tuple<string, string>, Tuple<string, string>> comp = new ListComparator<Tuple<string, string>, Tuple<string, string>>(list1, x => x.Item1, list2, a => a.Item1);
输出: 1个共同点 1新 1已删除
我的问题是当我用keyValuePair
替换元组时List<KeyValuePair<string, string>> list1 = new List<KeyValuePair<string, string>>();
List<KeyValuePair<string, string>> list2 = new List<KeyValuePair<string, string>>();
list1.Add(new KeyValuePair<string, string>("AA", "zefzef"));
list1.Add(new KeyValuePair<string, string>("A1", "iulyu"));
list2.Add(new KeyValuePair<string, string>("Abb", "szefez"));
list2.Add(new KeyValuePair<string, string>("A1", "zevzez"));
ListComparator<KeyValuePair<string, string>, KeyValuePair<string, string>> comp = new ListComparator<KeyValuePair<string, string>, KeyValuePair<string, string>>(list1, x => x.Key, list2, a => a.Key);
我得到了输入: commun 2
我不明白......为什么?怎么纠正呢? 感谢
答案 0 :(得分:2)
原因是KeyValuePair
是结构而不是类 - 意味着值而不是引用类型。这意味着default(KeyValuePair<string,string>)
不为空,因此行where item == null
和where item != null
无法按预期工作