NHibernate: QueryOver<> help

Posted by Andy Baker on Stack Overflow See other posts from Stack Overflow or by Andy Baker
Published on 2010-06-02T16:08:36Z Indexed on 2010/06/03 18:04 UTC
Read the original article Hit count: 844

Filed under:
|
|

Hi,

I'm just starting out with NHibernate and I'm having trouble with running more complex queries.

I have entities with a list of tags attached. The user will provide two lists of tags, include and exclude.

I need to find all the entities that have all of the include tags, and exclude any entites that have any tag in the exclude list.

Below is my first effort- which is clearly wrong as its listing all Display objects that have any of the include tags rather than all!

Any assistance is greatly appeciated.

var includeTagIds = (from tag in regime.IncludeTags select tag.Id).ToList<int>();
var excludeTagIds = from tag in regime.ExcludeTags select tag.Id;


var displays = session.QueryOver<Display>()
                      .JoinQueryOver<DisplayTag>(display => display.Tags)
                      .WhereRestrictionOn(tag => tag.Id)
                      .IsIn(includeTagIds).List().Distinct();


return displays.ToList();

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about nhibernate