CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Mar 2007
    Posts
    155

    Problem using List Except

    Code:
     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?
    I can't use Except method of List, why?

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: Problem using List Except

    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.

  3. #3
    Join Date
    Mar 2007
    Posts
    155

    Re: Problem using List Except

    I'm not getting any exception, it complies. But it's not getting the required values for list1.Except(list2).

    In your example you are using int, so it will work. If you could try with my class pattern, it won't

  4. #4
    Join Date
    May 2007
    Location
    Denmark
    Posts
    623

    Re: Problem using List Except

    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
    It's not a bug, it's a feature!

  5. #5
    Join Date
    May 2007
    Posts
    1,546

    Re: Problem using List Except

    You more than likely need to override the default equality method too:

    Code:
    public override bool Equals (object other)
    {
        return Equals (other as Customer);
    }
    www.monotorrent.com For all your .NET bittorrent needs

    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.

  6. #6
    Join Date
    Mar 2007
    Posts
    155

    Re: Problem using List Except

    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured