Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare to list of date in C#? [duplicate]

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 ?

like image 521
Priya Avatar asked Jul 31 '15 10:07

Priya


People also ask

How to compare dates in c?

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).

How do I compare two DateTime values?

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.

How do you compare dates in programming?

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.


2 Answers

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",

like image 70
Fabjan Avatar answered Sep 28 '22 04:09

Fabjan


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
like image 44
w.b Avatar answered Sep 28 '22 05:09

w.b