interface IValidationData : IEquatable<IValidationData>
{
}
class Customer : IValidationData
{
public string Name { get; set; }
public int Age { get; set; }
public bool Equals(IValidationData otherValue)
{
Customer other = otherValue as Customer;
if (!Name.Equals(other.Name))
return false;
if (!Age.Equals(other.Age))
return false;
return true;
}
public override int GetHashCode()
{
return Name.GetHashCode() ^ Age.GetHashCode();
}
}
List<Customer> list1 = new List<Customer>();
List<Customer> list2 = new List<Customer>();
List<Customer> list3 = list2.Except(list1).ToList();
// This line is not working why?
Without knowing the exact exception, it's hard to tell. Do you have using System.Linq; at the top of your source file? If not, add it.
The following code compiles for me okay:
Code:
List<int> list1 = new List<int>();
list1.Add(2);
list1.Add(3);
List<int> list2 = new List<int>();
list2.Add(2);
List<int> list3 = list1.Except(list2).ToList();
foreach(int i in list3)
Console.WriteLine(i);
//Prints 3
You can also try defining list3 like:
Code:
IEnumerable<int> diff = list1.Except(list2)
List<int> list3 = new List<int>(diff);
However, I'm not sure there is any advantage to doing that.
If neither of those solves your problem, post the exception message and that might help figure out what is wrong.
Best Regards,
BioPhysEngr http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
I did some debugging on your code. It seems if the objects in list1 are the same instances as the ones in list2, the code works. However, if the objects are different instances with the same values, it does not.
this works:
Code:
List<Customer> list1 = new List<Customer>();
Customer c = new Customer();
c.Age = 18;
c.Name = "Kitten";
list1.Add(c);
List<Customer> list2 = new List<Customer>();
//c = new Customer();
//c.Age = 18;
//c.Name = "Kitten";
list2.Add(c);
c = new Customer();
c.Age = 28;
c.Name = "Mitten";
list2.Add(c);
List<Customer> list3 = list2.Except(list1).ToList();
this does not:
Code:
List<Customer> list1 = new List<Customer>();
Customer c = new Customer();
c.Age = 18;
c.Name = "Kitten";
list1.Add(c);
List<Customer> list2 = new List<Customer>();
c = new Customer();
c.Age = 18;
c.Name = "Kitten";
list2.Add(c);
c = new Customer();
c.Age = 28;
c.Name = "Mitten";
list2.Add(c);
List<Customer> list3 = list2.Except(list1).ToList();
This happens despite the fact that the hashcodes appear to be identical. I'm not able to explain this behavior, but I hope this might help
NOTE: My code snippets are just snippets. They demonstrate an idea which can be adapted by you to solve your problem. They are not 100% complete and fully functional solutions equipped with error handling.
Thank you BioPhyEng, Foamy and Mutant fruit for answers.
Mutantfruit, Your solution worked.
Foamy, just found another interesting thing..
If I change the customer class to -
class Customer : IEquatable<Customer>
it's working too...
Looks like except doesn't want Equals method with parameter as an interface
Bookmarks