NHibernate QueryOver Alias Issue

2019-04-29 14:23发布

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?

1条回答
戒情不戒烟
2楼-- · 2019-04-29 14:53

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.

查看更多
登录 后发表回答