Sorry but there is no code for this question... I hope thats ok.
So, I have a list of objects with two properties, DateTime and String.
As a matter of fact, DateTime is just date (insertDate), for example 2022-02-04T00:00:00 01:00. String is of course some text, a lot of text :)
I have to compare the list with these two properties with another list of identical structure.
To be more clear, those are the data returned by the API, so I have to compare is there any differences between these two lists.
CodePudding user response:
Do it like this (it will compare two lists based on element index , so if your lists are unsorted you must sort them and then use these codes) :
public class YourClass
{
public DateTime DateTime { get; set; }
public string String { get; set; }
}
public class Program
{
static List<YourClass> list1 = new List<YourClass>
{
new YourClass { DateTime = DateTime.MinValue, String = "str1"},
new YourClass { DateTime = DateTime.Now.AddDays(1), String = "str2"},
new YourClass { DateTime = DateTime.Now.AddDays(2), String = "str3"}
};
static List<YourClass> list2 = new List<YourClass>
{
new YourClass { DateTime = DateTime.MinValue, String = "str1"},
new YourClass { DateTime = DateTime.Now.AddDays(-1), String = "2str"},
new YourClass { DateTime = DateTime.Now.AddDays(-2), String = "3str"}
};
static void Main(string[] args)
{
//a list of integer for storing different elements indexes
var differentElementsIndexes = new List<int>();
//check difference of these two list using for loop
for (int i = 0; i < list1.Count; i )
{
if (list1.ElementAt(i).DateTime != list2.ElementAt(i).DateTime || list1.ElementAt(i).String != list2.ElementAt(i).String)
differentElementsIndexes.Add(i);
}
}
CodePudding user response:
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());