score:2
Accepted answer
Using Queryover the solution should look like,
Tag tagAlias = new Tag();
Post postAlias = new Post();
Tag tagAliasInner = new Tag();
Post postAliasInner = new Post();
var subQuery = QueryOver.Of(() => postAliasInner)
.JoinAlias(() => postAliasInner.Tags, () => tagAliasInner)
.Where(Restrictions.EqProperty(Projections.Property(() => postAliasInner.Id),
Projections.Property(() => postAlias.Id)))
.Where(Restrictions.In(Projections.Property(() => tagAliasInner.Id), ids.ToArray()))
.Select(Projections.Count(Projections.Property(() => tagAliasInner.Id)));
var query = session.QueryOver(() => postAlias)
.JoinAlias(() => postAlias.Tags, () => tagAlias)
.Where(Restrictions.In(Projections.Property(() => tagAlias.Id), ids.ToArray()))
.WithSubquery.WhereValue(ids.Count).Eq<Post>(subQuery);
var results = query.List();
This results in SQL,
SELECT this_.Id as Id3_1_,
this_.Title as Title3_1_,
tags3_.Post_id as Post1_,
tagalias1_.Id as Tag2_,
tagalias1_.Id as Id5_0_,
tagalias1_.Text as Text5_0_
FROM "Post" this_
inner join PostTag tags3_ on this_.Id=tags3_.Post_id
inner join "Tag" tagalias1_ on tags3_.Tag_id=tagalias1_.Id
WHERE tagalias1_.Id in (?, ?)
and ? = (SELECT count(tagaliasin1_.Id) as y0_
FROM "Post" this_0_
inner join PostTag tags3_ on this_0_.Id=tags3_.Post_id
inner join "Tag" tagaliasin1_ on tags3_.Tag_id=tagaliasin1_.Id
WHERE this_0_.Id = this_.Id and tagaliasin1_.Id in (?, ?))
score:2
LINQ solution (nhibernate should be able to translate it)
var tags = new[] { 1 , 2 };
var postIds = PostTags
.Where(pt => tags.Contains(pt.TagId))
.GroupBy(pt => pt.PostId)
.Where(g => g.Count() == tags.Length)
.Select(g => g.Key);
SQL Solution:
SELECT PostId
FROM (
SELECT COUNT(*) AS count, PostId
FROM [PostTag]
WHERE TagId IN (1, 2) --List of tags
GROUP BY PostId
) as t1
WHERE [t1].[count] = 2 --Length of list
Explanation: We filter PostTag
to only include the tags we care about. Then we group by post. If the count of the grouping equals the length of the tag list, then the post contains all tags.
Source: stackoverflow.com
Related Query
- Find rows where child collection contains all elements of the list
- LINQ to EF - Find records where string property of a child collection at least partially matches all records in a list of strings
- Find all items whose collection property contains items in another list
- Find all items where child collection doesn't contain an item
- linq how to select the parent from a collection where the parent contains child items in another collection
- Query all the entities where list property contains everything in another list
- LINQ Lamba Select all from table where field contains all elements in list
- How to find keys in a Dict<int,List<Tuple<string,string>>> such that the list contains elements with given Item1 and Items
- List of objects where the object contains an array - how to use LINQ to search through all objects for a value in that array
- Linq - NHibernate - get all classes where the dictionary property contains one of the items from my list
- Use Linq to return all objects in a list where the object's sub-object-list property contains all values of an int list
- How to find all the rows that match a list of combinations in Linq/SQL?
- LINQ Where clause with Contains where the list has complex object
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- How to get all elements except the n'th element in a List using Linq
- Find all the common time from List of time provided by each user
- How to check if all of the elements in a list return true for a property using Linq?
- Entity Framework: Get all rows from the table for the ids in list
- Select Rows from a DataSet using LINQ, where the list of RowsID's are in a List<T>
- linq where contains ALL items in list
- How do I use Linq to find the elements of a list that are not present in another list?
- LINQ Remove all Users where UserId is in the list
- How do I use LINQ to find 5 elements in a row that match one predicate, but where the sixth element doesn't?
- Group to key-value pair where value is a list of elements that share the same key
- Using LINQ to get a list of items where the item contains a part of an item from another list
- Linq return all records from entity where a field contains one or more words from a list
- What is the best way to find all dependent children in a IEnumerable collection
- Update query in LINQ contains all columns in WHERE clause instead of just the primary key column
- C# Linq to XML getting the elements where value of name contains a specific string
- Elegant way to check if a list contains an object where one property is the same, and replace only if the date of another property is later
More Query from same tag
- Is it Possible to execute two LINQ queries of different types at the same time?
- Querying within other result query with linq
- Convert if else to linq
- LINQ group by date descending, and order by time ascending
- Join Subquery result in Linq
- Create nodes from List<XElement> using linq
- C# List to Dictionary without Foreach [ERROR]
- Linq Expression Get Predicated Items From Last With a Function
- Converting SQL to LINQ (left join on two fields)
- data continuously inserted in table
- C# linq sort - quick way of instantiating IComparer
- Joining items in a single List<>
- Adding new methods to LINQ to SQL generated classes
- LINQ Query to find where in collection on an included table
- Check One Date is falling Between Date Range-Linq query
- List convertion C# LINQ
- New to LINQ: Is this the right place to use LINQ?
- LINQ version of looping through array and performing multiple types of replacements
- Select single item from a list
- C# MVC: Func<Table1, "runtime type"> How do I get a dynamic type?
- Query a list object by a list int
- using If statement inside where clause in LINQ
- Cannot convert type 'System.Linq.IQueryable<double?>' to 'float'
- update list property using linq join on dictionary
- Join 2 table and group 2 field in linq
- Querying dynamic in asp.NET with SQL server
- Poker Hand evaluation through LINQ
- IQueryable, how to handle particular cases
- Displaying records for two weeks only! from creation
- Group By using Linq