views:

104

answers:

1

I'm building a complex security mechanism to filter access to objects depending on various rights.

As part of this I want to have an initial OR in my query that excludes all possible results from the query before the permissions allow access to certain subsets. In SQL it would look like this:

select   *
from     Table
where    (1 = 0)
or       ( /* various predicates */ )

However, I'm not sure how to create that initial 1 = 0 through the criteria API. I will probably need to create 1 = 1 at some point too but I'm assuming that will be a similar task.

How do I do this?

+1  A: 

Tuna Toksoz has suggested this method which is simple and clear:

var stuff = session.CreateCriteria<Stuff>()
    .Add(Restrictions.Eq(Projections.Constant(1), 0))
    .List<Stuff>();
Garry Shutler
but if you add more criteria with Add() they will actually AND with the 1=0
Mauricio Scheffer
Yes, but you would use it with a disjunction or similar. I'm just demonstrating the technique for achieving a criterion that eliminates all results.
Garry Shutler