Что ж, чтобы получить элементы, которые были обновлены между запусками, вам нужно написать новый IEqualityComparer для такого случая. В основном проверка того, что идентификатор остается таким же, как и был, но все остальное может быть изменено, например, имя, адрес и так далее. Вот пример такого компаратора с тестом - работает на моей стороне.
public class LocationIdEqualityComparer : IEqualityComparer<LocationData>
{
public bool Equals(LocationData x, LocationData y)
{
bool idComparer = string.Equals(x.Id, y.Id,
StringComparison.OrdinalIgnoreCase);
bool nameComparer = string.Equals(x.Name, y.Name,
StringComparison.OrdinalIgnoreCase);
bool addressComparer = string.Equals(x.Address, y.Address,
StringComparison.OrdinalIgnoreCase);
bool postcodeComparer = string.Equals(x.PostCode, y.PostCode,
StringComparison.OrdinalIgnoreCase);
// so you need to check that ID is the same, but everything else may be different
return idComparer && (!nameComparer || !addressComparer || !postcodeComparer);
}
public int GetHashCode(LocationData obj)
{
return obj.Id.GetHashCode();
}
}
class TestUpdatedItemsInList
{
[Test]
public void TestItemsAreUpdated()
{
List<LocationData> originalList = new List<LocationData>
{
new LocationData("1", "first", "somewhere1", "postCode1"),
new LocationData("2", "second", "somewhere2", "postCode2"),
new LocationData("3", "third", "somewhere3", "postCode3"),
new LocationData("4", "fourth", "somewhere4", "postCode4"),
};
List<LocationData> updatedList = new List<LocationData>
{
new LocationData("1", "1st", "somewhere1", "postCode1"),
new LocationData("2", "second", "who knows where", "postCode2"),
new LocationData("3", "third", "somewhere3", "updated postCode3"),
new LocationData("4", "fourth", "somewhere4", "postCode4"),
new LocationData("5", "fifth", "somewhere5", "postCode5"),
new LocationData("6", "sixth", "somewhere6", "postCode6"),
};
// newly added and updated items will end up here
var differentItems = updatedList.Except(originalList, new LocationFullEqualityComparer());
// only updated items will be here
var updatedItems = updatedList.Except(originalList, new LocationIdEqualityComparer());
// only non-changed items will be here (item 4)
var itemsWithoutChanges = updatedList.Intersect(originalList, new LocationFullEqualityComparer());
Assert.That(differentItems, Has.Exactly(5).Items);
Assert.That(updatedItems, Has.Exactly(3).Items);
Assert.That(itemsWithoutChanges, Has.Exactly(1).Items);
}
}
public class LocationData
{
public LocationData(string id, string name, string address, string postCode)
{
Id = id;
Name = name;
Address = address;
PostCode = postCode;
}
public string Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string PostCode { get; set; }
public override string ToString()
{
return $"{Id}, {Name}, {Address}, {PostCode}";
}
}
// code provided by you
public class LocationFullEqualityComparer : IEqualityComparer<LocationData>
{
public bool Equals(LocationData x, LocationData y)
{
bool idComparer = string.Equals(x.Id, y.Id,
StringComparison.OrdinalIgnoreCase);
bool nameComparer = string.Equals(x.Name, y.Name,
StringComparison.OrdinalIgnoreCase);
bool addressComparer = string.Equals(x.Address, y.Address,
StringComparison.OrdinalIgnoreCase);
bool postcodeComparer = string.Equals(x.PostCode, y.PostCode,
StringComparison.OrdinalIgnoreCase);
return idComparer && nameComparer && addressComparer && postcodeComparer;
}
public int GetHashCode(LocationData obj)
{
return obj.Id.GetHashCode();
}
}