我有一个曾经在 NHibernate LINQ 2.1.2 中工作的查询,但它使用 NH3 抛出 NotSupportedException:
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;
关系如下:
- 花参考茎
- 茎有许多花
- 叶参考词干
- 茎有许多叶子
从 NHibernate.Linq.Visitors.QueryModelVisitor 的第 204 行抛出异常。以下是源代码中的方法:
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);
}
在我看来,在以下线程下讨论了同样的问题:
在该线程下,Stefan 提到不支持该语法:
LINQ 提供程序期望表达式为:
查询源引用表达式。成员
但是,对于 from brw in loan.Application.Borrowers 它是:
查询源引用表达式。成员 。成员
所以这绝对是一个不受支持的功能。
那么,NH3 LINQ 是否随时支持这种语法?我认为这是一种微不足道的语法,拥有它很好。
但是我可以通过将查询重写为来解决这个问题:
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;
顺便说一句,有人有更好的解决方法吗?
nhusers 链接:http ://groups.google.com/group/nhusers/browse_thread/thread/334a53c749b0b377