Search code examples
c#listdatedatetimeexcept

How to compare to list of date in C#?


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 ?


Solution

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