-3

I have to find out the difference between two lists of class Category.

My Category class has these properties:

 public class Category
 {
    public int Id { get; set; }

    public string Title { get; set; }

    public bool IsQuantitative
    {
        get { return Products.Any(x => x.IsMultiPart); }
    }

    public List<Product> Products { get; set; }
    public string Image { get; set; }
    public string Description { get; set; }
}
Mikhail Gerasimov
  • 36,989
  • 16
  • 116
  • 159
G.Sharma
  • 31
  • 3
  • 4
    "difference" is a broad term. What *specific* difference are you concerned with? The order of the items? The number of items? The values of certain properties in each object? Something else? – rory.ap May 12 '16 at 12:03
  • How do you *define* the difference between the two lists? How do you define the difference between two instances of `Category` for that matter? You have to define what it is you *want* to do before you or anyone else can actually do it. – David May 12 '16 at 12:04
  • if the value of any list item dis matches, it should point out me that specific item. Does that make sence – G.Sharma May 12 '16 at 12:08
  • Great, so update your question to include that clarification. The comments is not the place for substantive clarifications. – rory.ap May 12 '16 at 12:09

2 Answers2

0

I would sort the two lists by Id first. Then run through it and compare the Objects. This is a good Object Comparer: https://www.nuget.org/packages/CompareNETObjects/

Here a little example:

            //Here you set the config like you want to have it compared
            ComparisonConfig comparisonConfig = new ComparisonConfig()
            {
                CompareChildren = true,
                CompareFields = true,
                CompareReadOnly = true,
                CompareProperties = true,
                MaxDifferences = 1,
                MaxByteArrayDifferences = 1
            };

            CompareLogic comparer = new CompareLogic() { Config = comparisonConfig };

            list1 = list1.OrderBy(x => x.Id).ToList();
            list2 = list2.OrderBy(x => x.Id).ToList();


            for (int i =0;i> list1.count;i++)
            {
                //Here you get a bool if the two Objects are Equal
                bool areEqual = comparer.Compare(list1[i], list2[i]).AreEqual;

                //Here you get a List of Differences Objects. It contains Values like "expected and "actual" etc.
                var differences = comparer.Compare(list1[i], list2[i]).Differences;

                //Here you handle Differences etc.
            }
Mr.Sheep
  • 311
  • 3
  • 16
0

Try this

var firstNotSecond = list1.Except(list2).ToList();

var secondNotFirst = list2.Except(list1).ToList();

link

Community
  • 1
  • 1
reza.cse08
  • 5,938
  • 48
  • 39