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 9098987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:29:59+00:00 2026-06-17T00:29:59+00:00

I have two Lists ListA<Emp> and ListB<Emp> both are having 1000 records. Emp is

  • 0

I have two Lists

ListA<Emp> and ListB<Emp>
both are having 1000 records.

Emp is an object of Employee Class. Below is my Employee class

public class Employee
{
    int ID = 0;
    string Name = String.Empty;
    string Dept = String.Empty;
    string Address = String.Empty;
    int Age = 0;
    string Email = String.Empty;
}

I want to verify if both the Lists are equal. The Emp objects may be placed in different order. Also, there might be several Emp objects which are having exactly same info in both the list. I have to verify those also.

I tried to sort the lists and compared using SequenceEqual

Enumerable.SequenceEqual(ListA.OrderBy(s => s), ListB.OrderBy(s => s)

I am getting below error

At least one object must implement IComparable.
Exception Stack trace is as below 

   at System.Collections.Comparer.Compare(Object a, Object b)
   at System.Collections.Generic.ObjectComparer`1.Compare(T x, T y)
   at System.Linq.EnumerableSorter`2.CompareKeys(Int32 index1, Int32 index2)
   at System.Linq.EnumerableSorter`1.QuickSort(Int32[] map, Int32 left, Int32 right)
   at System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count)
   at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__0.MoveNext()
   at System.Linq.Enumerable.SequenceEqual[TSource](IEnumerable`1 first, IEnumerable`1 second, IEqualityComparer`1 comparer)
   at System.Linq.Enumerable.SequenceEqual[TSource](IEnumerable`1 first, IEnumerable`1 second)

How can I implement this ? Also it will be better if you guys can provide me the fastest way of doing this because the number of objects in List may grow to 10 million.
Thanks for your help !

EDIT: Every employee must be in both list, order does not matter. But, if ListA contains same employee object 5 times (that means some duplicate entries), and ListB contains the employee object 4 times, then ListA and ListB are not equal.

  • 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-06-17T00:30:00+00:00Added an answer on June 17, 2026 at 12:30 am

    Best complexity is O(N)
    Following realization with using HashSet:

    Class with implementation of GetHashCode and Equals:

    public class Employee
    {
        public int ID = 0;
        public string Name = String.Empty;
        public string Dept = String.Empty;
        public string Address = String.Empty;
        public int Age = 0;
        public string Email = String.Empty;
    
        public override int GetHashCode()
        {
            return
                ID.GetHashCode() ^
                (Name ?? String.Empty).GetHashCode() ^
                (Dept ?? String.Empty).GetHashCode() ^
                (Address ?? String.Empty).GetHashCode() ^
                Age.GetHashCode() ^
                (Email ?? String.Empty).GetHashCode()
                ;
        }
        public override bool Equals(object obj)
        {
            Employee other = obj as Employee;
            if (obj == null)
                return false;
    
            return ID == other.ID &&
                    Name == other.Name &&
                    Dept == other.Dept &&
                    Address == other.Address &&
                    Age == other.Age &&
                    Email == other.Email;
        }
    }
    

    Function to compare lists:

    public static bool CompareLists(List<Employee> list1, List<Employee> list2)
    {
        if (list1 == null || list2 == null)
            return list1 == list2;
    
        if (list1.Count != list2.Count)
            return false;
        Dictionary<Employee, int> hash = new Dictionary<Employee, int>();
        foreach (Employee employee in list1)
        {
            if (hash.ContainsKey(employee))
            {
                hash[employee]++;
            }
            else
            {
                hash.Add(employee, 1);
            }
        }
    
        foreach (Employee employee in list2)
        {
            if (!hash.ContainsKey(employee) || hash[employee] == 0)
            {
                return false;
            }
            hash[employee]--;
        }
    
        return true;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two lists ... List<ObjectA> listA List<ObjectB> listB Both have a int property
I have two Lists, say listA and listA1 , listA contains object of class
I have two lists, ListA and ListB and I want to copy ListB to
I have two lists: List<int> listA List<int> listB How to check using LINQ if
Say I have two lists: List<CanidateSkill> canidateSkills; List<JobDesiredSkill> desiredSkills; class CanidateSkill { public Guid
I have two Sharepoint lists - ListA and ListB. ListA has columns productName, productType.
I have two lists (ObservableCollections) of following class: public class Data { public string
I have two lists: ListA: Brown Green Yellow Orange ListB: Yellow Orange I want
I have two python lists as follow: ListA = [a1,a2,a1,a3,a2,a4,a5,a4] ListB = [b1,b2,b1,b3,b2,b4,b5,b4] What
I have two lists: listA: [[Name: mr good, note: good,rating:9], [Name: mr bad, note:

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.