I'm finding lots of post on how to compare two List using except but I can't find anything explaining how to compare properties of two list.
I have two Models
FruitBasket
{
......
public int[] Fruit {get; set;}
}
Fruit
{
public int Id {get; set;}
......
}
The FruitBasket.Fruit
property has an array of numbers which corresponds to the Fruit.Id
. However there are orphans in the FruitBaskets
that I need to weed out.
For example FruitBasket.Fruit
would contain 1,2,3,4,5 but there would only be a Fruit 1, a Fruit 2, a Fruit 3. So I need to find 4 and 5 in the FruitBasket
and correct the FruitBasket
.
I have written an ugly dual ForEach
looping through FruitBaskets
and Fruit
trying to find the deltas and I'm getting outrageous results because I'm just not smart enough to figure it out.
List<int> missing = new List<int>();
foreach (var basket in lstBaskets)
{
foreach (var frt in lstFruits)
{
if (!basket.Fruit.Contains(frt.Id))
{
missing.Add(frt.Id);
}
}
}
Further there as got to be a more elegant way to accomplish this? Surely this is a common business need and there is a performant way of doing it?
Note
Note that one is a List property (FruitBaskets.Fruit) is being compared to Fruit which is just a List.