Odd Linq behavior with IList / IEnumerable
- by Aren B
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();
}
}
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?