I have the classic 3 table - entity, tag and entitytag - database structure.
In order to find all entities tagged with certain tags I am using the following Linqtosql code:
string[] myTags = {"tag1","tag2"};
var query = from m in entity
where m.entitytag.Where(c => myTags.Contains(c.tag.TagName)).Count() == myTags.Count()
select m;
However, when entities have duplicate tags(there is a valid reason for this in my real app) the query returns entities that do not match all tags.
eg in the above code sample, if an entity was tagged twice with 'tag1' and not 'tag2' it would return in the results despite not matching both tags.
I cant figure out how to exclude these entities from the results?
Or is there a completely different approach I should be taking?