Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6924371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:39:18+00:00 2026-05-27T10:39:18+00:00

I used to compare lists like this, but it returns false in a test:

  • 0

I used to compare lists like this, but it returns false in a test:

Assert.IsTrue(expected.SequenceEquals(actual));

And tried converting to json and it worked:

Assert.AreEqual(expected.ToJson(), actual.ToJson());

Values seems to be equal, what could be different? How to find out what is different in the lists?

Updated:

My class:

public class Department
{
    [BsonId]
    public ObjectId Id { get; set; }    
    public string Name { get; set; }            

    public override string ToString()
    {
        return Id.ToString();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:39:18+00:00Added an answer on May 27, 2026 at 10:39 am

    If MyClass implements IEquatable<MyClass>, then try this:

    expected.Sort(); 
    actual.Sort();
    if (Enumerable.SequenceEqual(actual, expected)) { ... }
    

    If it does not implement IEquatable then you could expect strange behavior, since the object references will be compared in the two lists, and not their fields:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class MyClassA
    {
      private int i;
      public MyClassA(int i) { this.i = i; }
    }
    
    public class MyClassB : IEquatable<MyClassB>
    {
      private int i;
      public MyClassB(int i) { this.i = i; }
      public bool Equals(MyClassB other) { return this.i == other.i; }
    }
    
    public class Program
    {
      public static void Main()
      {
        var actual1 = new List<MyClassA>() { new MyClassA(1), new MyClassA(2), new MyClassA(3) };
        var expected1 = new List<MyClassA>() { new MyClassA(1), new MyClassA(2), new MyClassA(3) };
        Console.WriteLine(Enumerable.SequenceEqual(actual1, expected1));
    
        var a1 = new MyClassA(1);
        var a2 = new MyClassA(2);
        var a3 = new MyClassA(3);
        var actual2 = new List<MyClassA>() { a1, a2, a3 };
        var expected2 = new List<MyClassA>() { a1, a2, a3 };
        Console.WriteLine(Enumerable.SequenceEqual(actual2, expected2));
    
        var actual3 = new List<MyClassB>() { new MyClassB(1), new MyClassB(2), new MyClassB(3) };
        var expected3 = new List<MyClassB>() { new MyClassB(1), new MyClassB(2), new MyClassB(3) };
        Console.WriteLine(Enumerable.SequenceEqual(actual3, expected3));
    
        var actual4 = new List<MyClassB>() { new MyClassB(1), new MyClassB(2), new MyClassB(3) };
        var expected4 = new List<MyClassB>() { new MyClassB(3), new MyClassB(2), new MyClassB(1) };
        Console.WriteLine(Enumerable.SequenceEqual(actual4, expected4));
      }
    }
    

    Output:

    False
    True
    True
    False
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Never used a cache like this before. The problem is that I want to
When used like this: import static com.showboy.Myclass; public class Anotherclass{} what's the difference between
I'd really like to handle this without monkey-patching but I haven't been able to
I know there are several questions named like this, but they don't seem to
Does anyone know of a tool that can be used to compare (relatively complex)
I just wanted to compare different solutions used when implementing ACLs in Rails.
I've seen this problem come up a lot, but never adequately handled, and I
Ok, I asked this question before, but deleted it as the way I went
I've used Textmate for a couple of years, but for Objective-C I'm finding that
Used to be quite happy with the Ctrl + Shift + F10 shortcut in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.