Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
Compare two lists and find the mismatch

I am having 2 lists:

List<DefectPopulation> lst_codeDefectSrc = new List<DefectPopulation>();
List<DefectPopulation> lst_codeDefectDest = new List<DefectPopulation>();

which are of type

private class DefectPopulation
{
    public int ClassCode { get; set; }
    public int DefectCount { get; set; }
}

Now I want to compare the two lists and find the mismatching item.

In the below if condition, it is saying mismatch is found but I want to know where it is:

List<DefectPopulation> lst_codeDefectSrc = new List<DefectPopulation>();
List<DefectPopulation> lst_codeDefectDest = new List<DefectPopulation>();

private class DefectPopulation
{
    public int ClassCode { get; set; }
    public int DefectCount { get; set; }
}

List<DefectPopulation> lst_diff = new List<DefectPopulation>();
if (lst_codeDefectSrc.Count == lst_codeDefectDest.Count)
{
    if (lst_codeDefectSrc.Except(lst_codeDefectDest).ToList().Any()
            && lst_codeDefectDest.Except(lst_codeDefectSrc).ToList().Any())
    {
        status = false;
    }
    else
    {
        status = true;
    }
}
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Change

if (lst_codeDefectSrc.Except(lst_codeDefectDest).ToList().Any()
        && lst_codeDefectDest.Except(lst_codeDefectSrc).ToList().Any())
...

To

var inSrcNotInDest = lst_codeDefectSrc.Except(lst_codeDefectDest).ToList();
var inDestNotInSrc = lst_codeDefectDest.Except(lst_codeDefectSrc).ToList();
if (inSrcNotInDest.Any() && inDestNotInSrc.Any()){
    // Use the respective list however you want
    lst_diff.AddRange(inSrcNotInDest);
    lst_diff.AddRange(inDestNotInSrc);

Note that the code above will use the default equality comparer, and that is reference comparison for class types. If you want value equality you should either write your own IEqualityComparer<DefectPopulation>, Implement IEquatable<DefectPopulation>, or change it to a struct, since that uses value equality per default.

over 4 years ago · Santiago Trujillo Report

0

This can be achieve in a following simple way.

If the intersection of two list has same number of elements as that of the lists that mean two lists are same.

You can use following code:

var commonItems = lst_codeDefectDest.Select(item => new { item.ClassCode, item.DefectCount })
                .Intersect(lst_codeDefectSrc.Select(item => new { item.ClassCode, item.DefectCount }));

status = commonItems.Count() == lst_codeDefectSrc.Count;

It is very well explained here : https://dotnettutorials.net/lesson/linq-intersect-method/

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!