C# 2 List<T> Karşılaştırma
-
Bu işi ilgili sınıfı kurcalamadan halletmenin en mantıklı yolu uygun yardımcı bir comparer sınıf yazıp ilgili IEnumerable metotlarını kullanmak.
Önce yardımcı sınıfını yaz:
public class AddressComparer : IEqualityComparer<Address> { public bool Equals(Address x, Address y) { if (x == null && y == null) return true; if (x == null || y == null) return false; return x.ID == y.ID && string.Equals(x.SourceKey, y.SourceKey); } public int GetHashCode(Address obj) { unchecked { return (obj.ID * 397) ^ (obj.SourceKey != null ? StringComparer.InvariantCulture.GetHashCode(obj.SourceKey) : 0); } } }
Sonra da şu şekilde ilgili listelerini oluştur:
var comparer = new AddressComparer(); // her iki listede de olanlar List<Address> updateList = eskiListe.Intersect(yeniListe, comparer).ToList(); // yalnız yeni listede olanlar List<Address> createList = yeniListe.Except(eskiListe, comparer).ToList(); // yalnız eski listede olanlar List<Address> deleteList = eskiListe.Except(yeniListe, comparer).ToList();
-
buzukatak bunu yazdı
Bu işi ilgili sınıfı kurcalamadan halletmenin en mantıklı yolu uygun yardımcı bir comparer sınıf yazıp ilgili IEnumerable metotlarını kullanmak.
Önce yardımcı sınıfını yaz:
public class AddressComparer : IEqualityComparer
{ public bool Equals(Address x, Address y) { if (x == null && y == null) return true; if (x == null || y == null) return false; return x.ID == y.ID && string.Equals(x.SourceKey, y.SourceKey); } public int GetHashCode(Address obj) { unchecked { return (obj.ID * 397) ^ (obj.SourceKey != null ? StringComparer.InvariantCulture.GetHashCode(obj.SourceKey) : 0); } } }Sonra da şu şekilde ilgili listelerini oluştur:
var comparer = new AddressComparer(); // her iki listede de olanlar List
updateList = eskiListe.Intersect(yeniListe, comparer).ToList(); // yalnız yeni listede olanlar ListcreateList = yeniListe.Except(eskiListe, comparer).ToList(); // yalnız eski listede olanlar ListdeleteList = eskiListe.Except(yeniListe, comparer).ToList();Aradığım şöyle bir çözümdü. Denemedim ancak uygulayıp deniycem teşekkürler