I have a list of questions, each linked to a list of tag.
And the following data :
Question1 : Tag1
Question2 : Tag1, Tag2
Question3 : Tag1, Tag2, Tag3
Question4 : Tag1, Tag3
The following criteria :
var tagsIds = new int[] { tag1, tag2 };
var res = session.CreateCriteria<Question>()
.CreateCriteria( "Tags" )
.Add( Restrictions.In( "id", tagsIds ) )
.List<Question>();
returns (I understand why, the "in" acts like an OR)
Question1, Question2, Question3, Question4
Or I would like to get only
Question2, Question3
as they both have tag1 AND tag2. I there a way to do it ?
In SQL, I would do something like :
SELECT *
FROM Question q
WHERE EXISTS (
SELECT *
FROM QuestionsToTags qtt
WHERE qtt.Question_id = q.Id
AND qtt.Tag_id IN ( 1, 2 )
GROUP BY qtt.Question_id
HAVING COUNT( qtt.Question_id ) >= 2
)