Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NHibernate QueryOver Alias Issue

I'm using the latest version of NHibernate (3.3.1.4000) from NuGet in .Net 4 targeted project in Visual Web Developer 2010 Express.

When I attempt to follow examples I've seen for defining aliases, I get an exception when setting them up using lambdas (see screenshot).

Shows error 'Cannot convert lambda expression to type 'string'...

As you can see I'm getting the error Cannot convert lambda expression to type 'string' because it is not a delegate type.

I have references to the LINQ namespaces in the top of my code:

using System.Linq;
using System.Linq.Expressions;

Any thoughts on what might be causing the problem?

like image 743
Sam Avatar asked Aug 21 '13 19:08

Sam


1 Answers

In order to use a variable like role in an expression, you have to define it first, like so...

Role roleAlias = null; // <-- these two lines are missing from your code.
Person personAlias = null; 

var x = session.QueryOver<Role>(() => roleAlias)
    .JoinAlias(r => r.People, () => personAlias)
    // ...

ISession.QueryOver<T>(...) has four overloads:

  • .QueryOver<T>()
  • .QueryOver<T>(Expression<Func<T>> alias)
  • .QueryOver<T>(string entityName)
  • .QueryOver<T>(string entityName, Expression<Func<T>> alias)

Apparently because it can't figure out what role is, it's assuming you're trying to use the .QueryOver<T>(string entityName) overload, hence the "Cannot convert ... to type 'string'" error message.

like image 141
Daniel Schilling Avatar answered Sep 21 '22 06:09

Daniel Schilling