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

4

7

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 ?

Secund answered 31/7, 2015 at 10:33 Comment(9)
Do you have to get the missing dates?Damselfly
Yes I have to get date which are missing @DamselflySecund
.Except would return all values except those that are in secondList. So it's something completely different from what you want.Alisha
What do you mean by "didn´t work"? What did you get instead of the desired result?Fernferna
@HimBromBeere see my comment ;)Alisha
@SteffenWinkler it's going to return the set difference so in this case a collection with a single item: 2015-07-24. Isn't it what you want Priya? Or do you want this to work both ways?Britannia
@vc, I want a collection with single item. (i.e) the one which is missingSecund
@vc74 well I wrote it wrong. It's not completely different but it's only half of the picture.Alisha
@Priya, then firstList.Except(secondList); should work. Are you getting any errors?Britannia
D
9

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

Domesticate answered 31/7, 2015 at 10:47 Comment(0)
P
3
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
Presumably answered 31/7, 2015 at 10:36 Comment(0)
F
0

You have to check if one list contains the values from the other list :

var anyOfthem = firstList.Where(x => !secondList.Contains(x));
Fernferna answered 31/7, 2015 at 10:37 Comment(0)
A
0

hope this is exactly what your looking for:

var notamatch= firstList.Where(x => !anyOfthem.Any(y => y.yourseconddatename== x.yourfirstdatename));
Authority answered 31/7, 2015 at 10:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.