2

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>();
Alexander Byndyu
  • 177
  • 1
  • 2
  • 8

1 Answers1

6

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.

hazzik
  • 13,019
  • 9
  • 47
  • 86
  • This query throws [NullReferenceException: Object reference not set to an instance of an object.] But works if change to: Session.QueryOver() .JoinAlias(p => p.Tags, () => tag) .WhereRestrictionOn(() => tag.Id) .IsIn(criterion.InterestedTags.Select(x => x.Id).ToArray()) .List(); – Alexander Byndyu May 30 '12 at 07:02
  • You need to select the exact field to compare. In this case the ID. So in the IsIn method we need the Id. IsIn(criterion.InterestedTags.Select(x => x.Id)).ToList() – maxspan Mar 05 '15 at 03:13