0

I've written an application that uses LINQ to NHibernate for it's database queries. Code in my domain layer creates expressions of type

System.Linq.Expressions.Expression<Func<T, bool>>

These are passed to repositories in my data access layer, which then use them like this:

return session.Query<T>.Where(expression)...

This was all great before I discovered that NHibernate LINQ provider completely ignores the option fetch="join" in mapping files, meaning that objects are fetched using several select statements rather than a single select with joins.

So I'm trying to move over to the QueryOver API, which fully supports joins. I immediately ran into the problem that methods like string.Equals are not supported. I also have several custom extension methods which I use in my expressions, for which I have added support for in LINQ to NHibernate by extending BaseHqlGeneratorForMethod.

How can I add support for these methods in the QueryOver API? I can't find any information on this.

I'm aware you can do things like this:

Session.QueryOver<T>().WhereRestrictionOn(x=>x.Foo).IsInsensitiveLike("bar")

but I'm looking for a way to do everything using the same expressions that I was using before with LINQ to NHibernate.

Dominic
  • 481
  • 2
  • 10
  • add lazy="false" to fetch="join" and it should work with LINQ 2 NH – Firo Feb 20 '13 at 07:08
  • Are you sure? I'm using NHibernate 3.3 and have lazy="false" fetch="join" in my mapping, but it fetches using multiple select statements. It'll uses a single select statement with joins if I call session.Get(id), but not when I call session.Query(). – Dominic Feb 20 '13 at 07:59
  • Linq2NH is the linq provider. QueryOver is not another linq provider, it just uses lambda expression to reference properties of your model. – Oskar Berggren Feb 20 '13 at 08:22
  • are there any filter conditions on the associations when using `Query()`? – Firo Feb 20 '13 at 08:38
  • @Oskar: Although QueryOver isn't a linq provider, I thought it could still use lamda expressions for filtering. If not, then what does session.QueryOver().Where(Expression>) exist for? – Dominic Feb 20 '13 at 08:55
  • @Firo: I get multiple select statements even if I provide no filter conditions at all. For example, if I call session.Query().ToList() to fetch all elements, separate select statements are issued to populate properties of one-to-many associations, even though these one-to-many associations are specified with lazy="false" fetch="join". – Dominic Feb 20 '13 at 09:14
  • Why can't you use the eager fetching extension methods (`Fetch()`, `FetchMany()`, `ThenFetch()` and `ThenFetchMany()`)? – cremor Feb 20 '13 at 09:16
  • 1
    @Dominic I think it support only a very limited set of expressions, such as `t => t.Name == someVariable`. – Oskar Berggren Feb 20 '13 at 09:19
  • @cremor: To use Fetch() etc my repositories would have to have knowledge of the structure of the query. But they don't, they simply receive lambda expressions from calling code. I'm starting to think that it's not possible to specify query conditions using lambda expressions while enjoying support for fetch="join". – Dominic Feb 20 '13 at 10:15

1 Answers1

0

The answer seems to be that it's not possible to use lambda expressions with the QueryOver API. As Oskar pointed out in a comment to the question, only very simple lambda expressions are supported. I'm rewriting my code so that the repository methods receive query objects rather than lambda expressions.

Dominic
  • 481
  • 2
  • 10