I've got the following code:
public IList<IProductViewModel> ChildProducts { get; set; }
public IList<IProductViewModel> GiftItems { get; set; }
public IList<IProductViewModel> PromoItems { get; set; }
public IList<IProductViewModel> NonGiftItems
{
get
{
return NonPromoItems.Except(GiftItems, new ProductViewModelComparer()).ToList();
}
}
public IList<IProductViewModel> NonPromoItems
{
get
{
return ChildProducts.Where(p => !p.IsPromotion).ToList();
}
}
ProductViewModelComparer:
public class ProductViewModelComparer : IEqualityComparer<IProductViewModel>
{
#region IEqualityComparer<IProductViewModel> Members
public bool Equals(IProductViewModel x, IProductViewModel y)
{
if (Object.ReferenceEquals(x, y))
return true;
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
return String.Equals(x.ProductId, y.ProductId);
}
public int GetHashCode(IProductViewModel obj)
{
return obj.ProductId.GetHashCode();
}
#endregion
}
So basically, NonPromoItems
is (ChildProducts - PromoItems)
and NonGiftItems
is (NonPromoItems - GiftItems)
However When:
ChildProducts
= IEnumerable<IProductViewModel>[6]
PromoItems
= IEnumerable<IProductViewModel>[1]
where item matches 1 item in ChildProducts
GiftItems
= IEnumerable<IProductViewModel>[0]
My Result is
NonPromoItems
= IEnumerable<IProductViewModel>[5]
This is Correct
NonGiftItems
= IEnumerable<IProductViewModel>[4]
This is Incorrect
Somehow an Except(...)
is removing an item when given an empty list to subtract.
Any ideas anyone?