0

I have next code

if (user != null)
  query = query.Where(item => item.User.Id == user.Id);
else if (equipment != null)
  query = query.Where(item => false);// TODO: Add logic for equipment

but NHibernate can't build expression tree for item => false. Someday this expression will change, but now it must return empty query. Is there some method to solve this problem?

3
  • WHERE(1 = 0) will work Commented May 8, 2013 at 15:02
  • @stuartd , no. same exception. "Could not determine member type from Constant, False, System.Linq.Expressions.ConstantExpression" Commented May 8, 2013 at 15:05
  • sorry, I was sure I'd used that in the past. Commented May 8, 2013 at 16:04

2 Answers 2

1

Assuming it is an IQueryable of User, try this.

if (user != null)
  query = query.Where(item => item.User.Id == user.Id);
else if (equipment != null)
  query = new List<User>().AsQueryable();

Edit:

OP said it is a IQueryOver not an IQueryiable, had a look at source and QueryOver is protected so can't see how to create an empty one easily right now. My suggestion is to use the LINQ provider if you can so my original answer will work. If it's not possible then this UGLY hack will work for now.

if (user != null)
                query = query.Where(item => item.User.Id == user.Id);
            else if (equipment != null)
                query = query.Where(item => item.User.Id < 0 && item.User.Id > 1); 
Sign up to request clarification or add additional context in comments.

3 Comments

AsQueryable() returns IQueryable object, but query is IQueryOver =(
Assumption is the mother of all mistakes. Is there a reason you're not using nHibernate's LINQ provider? Generally much nicer if not as powerful. There may be a nicer way but for now you can do this. if (user != null) query = query.Where(item => item.User.Id == user.Id); else if (equipment != null) query = query.Where(item => item.User.Id < 0 && item.User.Id > 1);
Thanks! Id is Guid, so I use next expression: query = query.Where(item => item.Id == Guid.Empty);
0

I think I've used

  Where(item => 1 == 0)

1 Comment

Same exception. "Could not determine member type from Constant, False, System.Linq.Expressions.ConstantExpression". Probably, NHibernate calculates expressions with constants, so we get ours Where(item=>false);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.