I have this expression:
var result = from pav in ProductAttributes
join id in valueIds
on pav.AttributeValueID equals id
select pav.ProductImageID;
which works up to a point. The issue is that the collection ProductAttributes contains the same product many times, for each attribute. It’s structure is:
ID – unique
ProductID
ProductAttributeValueID
ProductImageID
So a Product may appear many times in the collection. I want the result to actually filter OUT all the products that DON’T have any matches at all in valueIds (which is a list of ProductAttributeValueIDs).
So I want to ONLY return products that have ALL of the COMBINED valueIds, not just ANY of them, which is what the above linq expression is doing.
PS I can post SQL code that shows what I mean in SQL if that helps!
@devgeezer posted an answer which was close enough but it only worked for one value.
I ended up with the code below, which works. I group on the ProductID, then use that in a 2nd query to filter the original collection:
var result =
from pav in ProductAttributeValues
join id in valueIds
on pav.AttributeValueID equals id
group pav by pav.ProductID into gj
where gj.Count() == valueIds.Count()
select gj.Key;
var imageIds = from pav in ProductAttributeValues
join id in result
on pav.ProductID equals id
select pav.ProductImageID;
You might try a group and filter approach something like the following: