Queryover where id is not in list

Dofs picture Dofs · Feb 25, 2012 · Viewed 9.9k times · Source

I have been struggling with this for a while, so I hope some of you QueryOver experts can help.

I have a list of blog posts. You can vote on each blog post, and I would like (among other) to recieve a list of posts where the user hasn't voted.

First I was thinking about doing something like:

Session.QueryOver<BlogPost>()
.WhereRestrictionOn(bp => bp.Id)
.NotIn(existingBlogPostVotes);

(existingBlogPostVoteIds is the ids of the voted blogposts)

But this doesn't exist in the QueryOver framework.

I found out that I could do it in Criteria like this:

var crit =
     Session.CreateCriteria<BlogPost>()
     .Add(Restrictions.Not(Restrictions.In("Id",existingBlogPostVotes)));

But I would to do this in QueryOver and not Criteria.

How would this be done in QueryOver?

Answer

Cole W picture Cole W · Feb 26, 2012

How about Not.IsIn():

   Session.QueryOver<BlogPost>()
          .WhereRestrictionOn(bp => bp.Id)
          .Not.IsIn(existingBlogPostVotes);

Optionally this could be done in the Linq provider as well:

   Session.Query<BlogPost>()
          .Where(bp => !existingBlogPostVotes.Contains(bp.Id));