0

I'm trying to compare two lists using CollectionAssert but it failed in comparing exact match and also it is not telling which value is incorrect

List<string> ExpectedList = new List<string>() { "apple","orange","grapes","mango"};
List<string> ActualList = new List<string>() { "gova","orange","GRAP"};
CollectionAssert.AreEqual(ExpectedList, ActualList)

Expected results should be in String:

"apple gova, grape GRAP, empty Mango"

How can I do it more efficiently or simply? Is there any other Assertion available in C#?

stan
  • 81
  • 1
  • 12
  • What are the actual results? post the assertion code as well. – Guy Oct 29 '18 at 06:46
  • possible duplicate: https://stackoverflow.com/questions/675699/compare-two-lists-for-differences – jazb Oct 29 '18 at 06:48
  • @Guy - have just added it. – stan Oct 29 '18 at 06:50
  • 1
    `it is not telling which value is incorrect` It **is** telling you the first value that is incorrect (`Message: Expected is with 4 elements, actual is with 3 elements Values differ at index [0] Expected string length 5 but was 4. Strings differ at index 0. Expected: "apple" But was: "gova"`). Why do you want more than that? – mjwills Oct 29 '18 at 06:55
  • Why is `mango` not in the list of mismatches? What **exact** result do you expect if one list is empty and the other has 3 entries in it? – mjwills Oct 29 '18 at 06:55

1 Answers1

3

Use Zip method like this:

List<string> ExpectedList = new List<string>() {"apple", "orange", "grapes", "mango"};
List<string> ActualList = new List<string>() {"gova", "orange", "GRAP"};

var result = ExpectedList.Zip(ActualList, (first,second) => first != second ?
        $"Mismatch = {first} , {second}" :  "")
            .Concat(ExpectedList.Skip(ActualList.Count))
            .Concat(ActualList.Skip(ExpectedList.Count))
            .Where(c=>!string.IsNullOrWhiteSpace(c)).ToList();

And if you want to get the result as string:

string theStringVersion = string.Join(",", result);
Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
  • Thanks for your Solution and Effort. 1 item is pending! "Mango" should also be mentioned in mismatch records too – stan Oct 29 '18 at 07:22
  • 1
    fantastic, highly appreciate you help and the prompt response. – stan Oct 29 '18 at 07:43
  • is there any easiest way to convert this var to String ? – stan Oct 29 '18 at 07:44
  • @stan Yes, you can use `String.Join` like this `string theStringVersion = string.Join(",", result);` – Salah Akbari Oct 29 '18 at 07:48
  • It is worth mentioning that if your lists are `{"apple", "orange", "grapes", "mango"}` and `{"orange", "grapes", "mango"}` that this will consider there to be four errors (positions 0-3 are all mismatches) rather than one error (apple is missing from list 2). That is the list will only check if entries in specific positions match, not a more complicated version of "What are the differences between the two lists" that might spot that there is in fact just one missing entry. The latter is very hard to do though... – Chris Oct 30 '18 at 11:03
  • @Chris The OP clearly stated that he wants to compare for exact match of two lists. He wants to compare **the corresponding elements of two sequences** based on his input and his desired result. He also updated his question with a more close title to his needs. – Salah Akbari Oct 30 '18 at 11:10
  • @SalahAkbari - In one scenario the solution failed! - i want the result which tells me mismatch and also should shows the empty value in the second list(ActualLIst) ? – stan Mar 28 '19 at 10:41
  • @stan It has passed a lon time since this question has been asked. I believe that you need to keep this question accepted and start another thread and ask a new question by referencing to this one and clarify your needs more on that newly created thread. Good Luck. – Salah Akbari Mar 28 '19 at 14:07