-2

I have two collections ProdList<Product, int> and RuleList<Product, int> contains list of products and counts.

how do i get if RuleList all items partici in ProdList collection I ve tried with

bool a= ProdList.All(x => RuleList.Contains(x)); 

but it does not work.

3
  • You should override the Equals method and then your code will work
    – rafaelc
    Commented Aug 31, 2016 at 21:36
  • What's ProdList and RuleList? Dictonaries i assume? Are you trying to match product or the int? or both? Commented Aug 31, 2016 at 21:37
  • @SledgeHammer both Commented Aug 31, 2016 at 21:39

1 Answer 1

1

The reason it doesn't work is because the Contains method uses the Equals and GetHashCode of the items, which if not overriden, are those of object.

Solutions:

  1. Override your object's Equals and GetHashCode
  2. Use Any instead of Contains:

    var result = ProdList.All(x => RuleList.Any(y => x.Product.Equals(y.Product) && 
                                                     x.IntValue == y.IntValue));
    
  3. Implement a custom IEqualityComparer for your object's type

Notice that in all these options you must also take care of comparing the Product class (same 3 options are available)

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.