I've got problem with NHibernate v3.2.0, and following query:
class DocumentBase {}
class Requisition: DocumentBase {}
class Order: DocumentBase {}
Repository.GetAll<DocumentBase>()
.Where(d => (d is Requisition) && ((Requisition)d).ProductItem != null)
Base query is designed to list all documents, but there is also possibility to filter this documents by type (and quasi-type, which is for example document without product). In code above there is only one condition, but predicate can be more complicated, for ex:
Repository.GetAll<DocumentBase>()
.Where(d =>
((d is Requisition) && ((Requisition)d).ProductItem != null) ||
(d is Order) ||
[...]
)
When executed I receive InvalidPathException
with message Invalid path: 'd.ProductItem'
.
Any ideas? Is it supported?
So far I managed to bypass this error with following query:
Repository.GetAll<DocumentBase>()
.Where(d =>
(d is Requisition) &&
Repository.GetAll<Requisition>()
.Any(r => r.Id == d.Id && r.ProductItem != null)
)
But definitely it's not the best choise in terms of performance.
As someone mentioned it is possible to extend Linq provider in NHibernate. Here is solution which worked for me:
Example query:
I guess that your
Repository.GetAll<T>
method is returningsession.Query<T>
. There is only a limited subset of expressions you can do have yourWhere
clause. NHibernate tries to convert your Where expression to HQL, and eventually to SQL. This means that you cannot write anything in Where and expect it to work, but only those expressions for which the conversion exists. However, NHibernate's Linq provider can be extended.Your case can be simplified. If you have your subclasses mapped... and you probably do, the query can be written as: