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: