I have something similar to this model:
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
public ICollection<ProductVariation> Variations { get; set; }
}
public class ProductVariation
{
public int VariationID { get; set; }
public string Size { get; set; }
public bool InStock { get; set; }
}
I have a List of Product – called “availableProducts”
List<Product> availableProducts;
I then also have a List of int – requestedVariationNumbers:
List<int> requestedVariationNumbers;
What I’m trying to do is get Products from availableProducts where the Product.Variations collection contains a VariationID that is in requestedVariationNumbers.
So far, I’ve got this:
(Generated by ReSharper based on a series of ugly foreach statements…)
It just seems “dirty” to me…
var result = (
from rvn in requestedVariationNumbers
from product in availableProducts
from itemVariation in product.ItemVariations
where itemVariation.ItemNo == rvn
select product)
.ToList();
Try this:
This is not really efficient. It would be better if
requestedVariationNumberswould be aHashSet, becauserequestedVariationNumbers.Contains(v.VariationID)is called for every variation in every product in the worst case and for every first variation of every product in the best case.