Alexander Byndyu
Alexander Byndyu

Reputation: 187

NHibernate QueryOver and Collection Filtering

Simple example of my class:

public class Post
{
    public IEnumerable<Tag> Tags { get; set; }
}

User checks few interested tags for filtering Post list.

I need to filter all post by selected tags like:

Session.QueryOver<Post>()
    .WhereRestrictionOn(x => x.Tags)
    .IsIn(criterion.InterestedTags.ToList())
    .List<Post>();

Exception: NHibernate.QueryException: Cannot use collections with InExpression

Actually, I should show post if one of its tag contains in InterestedTags.

UPD

Works for me:

Session.QueryOver<Post>()
    .JoinAlias(p => p.Tags, () => tag)
    .WhereRestrictionOn(() => tag.Id)
    .IsIn(criterion.InterestedTags.Select(x => x.Id).ToArray())
    .List<Post>();

Upvotes: 2

Views: 6788

Answers (1)

hazzik
hazzik

Reputation: 13344

You have to use alias to make restrictions on one-to-many part

Try following code snippet:

Tag tag = null; 
Session.QueryOver<Post>()
    .JoinAlias(p => p.Tags, () => tag)
    .WhereRestrictionOn(() => tag.Id)
    .IsIn(criterion.InterestedTags.ToList()) //*
    .List<Post>();

*Assuming that InterestedTags is collection of identifiers.

Upvotes: 6

Related Questions