I have a query that used to work in NHibernate LINQ 2.1.2 but it is throwing NotSupportedException with NH3:
IQueryable<Tree> query = from flower in GetSession().Query<Flower>()
from leaf in flower.Stem.Leaves // <--- the problem is here with three jumps
where leaf.Color == Green
select flower;
The relations are like:
- Flower References Stem
- Stem HasMany Flowers
- Leaf References Stem
- Stem HasMany Leaves
The exception is thrown from line 204 in NHibernate.Linq.Visitors.QueryModelVisitor. Here is the method from the source code:
public override void VisitAdditionalFromClause(AdditionalFromClause fromClause, QueryModel queryModel, int index)
{
if (fromClause is LeftJoinClause)
{
// It's a left join
_hqlTree.AddFromClause(_hqlTree.TreeBuilder.LeftJoin(
HqlGeneratorExpressionTreeVisitor.Visit(fromClause.FromExpression, VisitorParameters).AsExpression(),
_hqlTree.TreeBuilder.Alias(fromClause.ItemName)));
}
else if (fromClause.FromExpression is MemberExpression)
{
var member = (MemberExpression) fromClause.FromExpression;
if (member.Expression is QuerySourceReferenceExpression)
{
// It's a join
_hqlTree.AddFromClause(_hqlTree.TreeBuilder.Join(
HqlGeneratorExpressionTreeVisitor.Visit(fromClause.FromExpression, VisitorParameters).AsExpression(),
_hqlTree.TreeBuilder.Alias(fromClause.ItemName)));
}
else
{
// What's this?
throw new NotSupportedException(); // <--------- LINE 204
}
}
else
{
// TODO - exact same code as in MainFromClause; refactor this out
_hqlTree.AddFromClause(_hqlTree.TreeBuilder.Range(
HqlGeneratorExpressionTreeVisitor.Visit(fromClause.FromExpression, VisitorParameters),
_hqlTree.TreeBuilder.Alias(fromClause.ItemName)));
}
base.VisitAdditionalFromClause(fromClause, queryModel, index);
}
It seems to me the same issue is discussed under the following thread:
Under that thread Stefan mentions that the syntax is not supported:
The LINQ provider expects the expression to be:
QuerySourceReferenceExpression . Member
However, in the case of from brw in loan.Application.Borrowers it is:
QuerySourceReferenceExpression . Member . Member
So it's definately an unsupported feature.
So, is this syntax going to be supported anytime in NH3 LINQ? I think it is a trivial syntax and it's good to have.
However I can go around this issue by rewriting the query as:
IQueryable<Tree> query = from stem in GetSession().Query<Stem>()
from leaf in stem.Leaves
from flower in stem.Flowers
where leaf.Color == Green
select flower;
BTW, anyone has a better workaround?
nhusers link: http://groups.google.com/group/nhusers/browse_thread/thread/334a53c749b0b377