2
class Customer : IEquatable<Customer>
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string ZipCode { get; set; }

    public bool Equals(Customer other)
    {
        if (ReferenceEquals(null, other)) return false;

        return this.FirstName.Equals(other.FirstName) && this.LastName.Equals(other.LastName)
            && this.ZipCode.Equals(other.ZipCode); 
    }

    public override bool Equals(object obj)
    {
        return this.Equals(obj as Customer);
    }

    public override int GetHashCode()
    {
        return this.FirstName.GetHashCode() ^ this.LastName.GetHashCode();
    }
}

static void Main(string[] args)
{
    List<Customer> allCustomers = new List<Customer>();
    allCustomers.Add(new Customer { FirstName = "A", LastName = "V", ZipCode = "11111" });
    allCustomers.Add(new Customer { FirstName = "B", LastName = "W", ZipCode = "11111" });
    allCustomers.Add(new Customer { FirstName = "C", LastName = "X", ZipCode = "22222" });
    allCustomers.Add(new Customer { FirstName = "D", LastName = "Y", ZipCode = "33333" });
    allCustomers.Add(new Customer { FirstName = "E", LastName = "Z", ZipCode = "33333" });

    List<Customer> subList = new List<Customer>();
    subList.Add(new Customer { FirstName = "A", LastName = "V", ZipCode = "11111" });
    subList.Add(new Customer { FirstName = "B", LastName = "W", ZipCode = "11111" });
    subList.Add(new Customer { FirstName = "C", LastName = "X", ZipCode = "22222" });

    //This gives expected answer
    var n = subList.Except(allCustomers).ToList();

    //This should compare only for those customers who zip matches with Sublist's zip
    //This returns customers with zip code "33333" while it should not
    var v = allCustomers.Except(subList).ToList();
}

var V is not supposed to return customer with zipcode "33333". How do i compare so that it ignores those Customers whose Zip is not present in the Sublist?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Asdfg
  • 11,362
  • 24
  • 98
  • 175
  • This would be a classic case for a LINQ Join. I happen to loath the syntax for Linq Joins, which leads to the fact that I won't be able come up with the answer from memory :) – sehe Apr 25 '11 at 19:35
  • Also, I think it would be better if the hashcode included the zipcode; see also http://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode for generall advice on implementing GetHashCode – sehe Apr 25 '11 at 19:41
  • This seems to be working correctly to me. {A, B, C, D, E} - {A, B, C} = {D, E}... am I missing something here? – James Michael Hare Apr 25 '11 at 19:41
  • True on hashcode, though as long as the hashcode can't be different when the two items are equal, it isn't wrong (though it won't be as optimal). Since both equals and hashcode use first and last name, it should at least be consistent. – James Michael Hare Apr 25 '11 at 19:42
  • Oh i see, so you just want to select all customers who's zip only isn't present in the sublist? Or zip and name? – James Michael Hare Apr 25 '11 at 19:46
  • if the Zip isnt present, that record should be ignored. If the Zip matches, it should check if there is any customer who is missing or is extra in either list – Asdfg Apr 25 '11 at 19:48
  • So, you want the difference both ways, then. You want the Union minus the intersection but only in the case of names and not in the case of zips. Is that right? – James Michael Hare Apr 25 '11 at 19:51

2 Answers2

4
 var zipCodes = subList.Select( c=> c.ZipCode).Distinct().ToList();
 var v = allCustomers.Where( c=> zipCodes.Contains(c.ZipCode) )
                     .Except(subList)
                     .ToList();
BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
0

Why are you saying V should not include zipcode 33333? allCustomers - subList would remove the common elements but keep the unique elements of allCustomers.

It's a set difference, so {A, B, C, D, E} - {A, B, C} = {D, E} thus the 33333 should show...

Seems similar to this example from MSDN: http://msdn.microsoft.com/en-us/library/bb300779.aspx

James Michael Hare
  • 37,767
  • 9
  • 73
  • 83
  • Okay, so I'm guessing it's not a question of Except but of how to get the elements the user wants. My answer was more in reference to "var V is not supposed to return customer with 33333 zip" as if Except wasn't working right. I think now in retrospect it's a question of what query WILL give what he wants. – James Michael Hare Apr 25 '11 at 19:47