You can use a CustomComparator:
Given your base class for both lists:
public class BaseClass
{
public DateTime date { get; set; }
public string anyString { get; set; }
}
Define the custom comparer for it
public class BaseClassComparer : IEqualityComparer<BaseClass>
{
public bool Equals(BaseClass item1, BaseClass item2)
{
//Check whether the compared objects reference the same data.
if (Object.ReferenceEquals(item1, item2)) return true;
//Check whether any of the compared objects is null.
if (Object.ReferenceEquals(item1, null) || Object.ReferenceEquals(item2, null))
return false;
//Check whether the required fields' properties are equal.
return (item1.date == item2.date) && (item1.anyString == item2.anyString);
}
public int GetHashCode(BaseClass item)
{
//Check whether the object is null
if (Object.ReferenceEquals(item, null)) return 0;
//Get hash code for the Date field if it is not null.
int hashItemDate = item.date == null ? 0 : item.date.GetHashCode();
//Get hash code for the string field.
int hashItemString = item.anyString.GetHashCode();
//Calculate the hash code for the item.
return hashItemDate ^ hashItemString;
}
}
Then you can use it as follows:
List<BaseClass> class1 = new List<BaseClass>();
List<BaseClass> class2 = new List<BaseClass>();
BaseClass item1 = new BaseClass() {date = DateTime.Now, anyString = "Hello"};
class1.Add(item1);
class2.Add(item1); //<-Equal till here
//class1.Add(item1); //<-uncomment for false
bool result = class1.SequenceEqual(class2, new BaseClassComparer());