I have two list of date. I have to compare both list and find missing date. My first list looks like this:
2015-07-21
2015-07-22
2015-07-23
2015-07-24
2015-07-25
2015-07-26
2015-07-27
My second list looks like this
2015-07-21
2015-07-22
2015-07-23
2015-07-25
2015-07-26
2015-07-27
I have to find the missing date between the two list :
I tried this
var anyOfthem = firstList.Except(secondList);
But it didn't work. Can anyone help me with this ?
Consider the problem of comparison of two valid dates d1 and d2. There are three possible outcomes of this comparison: d1 == d2 (dates are equal), d1 > d2 (date d1 is greater, i.e., occurs after d2) and d1 < d2(date d1 is smaller, i.e., occurs before d2).
The DateTime. Compare() method in C# is used for comparison of two DateTime instances. It returns an integer value, <0 − If date1 is earlier than date2.
For comparing the two dates, we have used the compareTo() method. If both dates are equal it prints Both dates are equal. If date1 is greater than date2, it prints Date 1 comes after Date 2. If date1 is smaller than date2, it prints Date 1 comes after Date 2.
Well, you could use .Except()
and .Union()
methods :
string[] array1 =
{
"2015-07-21",
"2015-07-22",
"2015-07-23",
"2015-07-24",
"2015-07-25",
"2015-07-26",
};
string[] array2 =
{
"2015-07-21",
"2015-07-22",
"2015-07-23",
"2015-07-25",
"2015-07-26",
"2015-07-27"
};
var result = array1.Except(array2).Union(array2.Except(array1));
foreach (var item in result)
{
Console.WriteLine(item);
}
Output : "2015-07-24", "2015-07-27",
string[] array1 =
{
"2015-07-21",
"2015-07-22",
"2015-07-23",
"2015-07-24",
"2015-07-25",
"2015-07-26",
};
string[] array2 =
{
"2015-07-21",
"2015-07-22",
"2015-07-23",
"2015-07-25",
"2015-07-26",
"2015-07-27"
};
var common = list1.Intersect(list2);
var anyOfThem = list1.Except(common).Concat(list2.Except(common));
foreach (var date in anyOfThem)
Console.WriteLine(date);
// 2015-07-24
// 2015-07-27
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With