Permalink
Browse files

Linq - fix incorrect behavior when using fetch request with SingleOrD…

…efault/Single/FirstOrDefault/First operators (NH-3186)
  • Loading branch information...
1 parent c866d11 commit 806cf928dd09531a1805a031a28c378db342510a @hazzik hazzik committed Jun 11, 2012
@@ -288,5 +288,20 @@ public void WhereBeforeFetchManyThenFetchAndWhere()
Assert.IsTrue(NHibernateUtil.IsInitialized(orders[0].OrderLines));
Assert.IsTrue(NHibernateUtil.IsInitialized(orders[0].OrderLines.First().Product));
}
+
+ [Test]
+ public void WhereAfterFetchAndSingleOrDefault()
+ {
+ //NH-3186
+ var firstOrderId = db.Orders.OrderBy(x => x.OrderId)
+ .Select(x => x.OrderId)
+ .First();
+
+ var order = db.Orders
+ .Fetch(x => x.Shipper)
+ .SingleOrDefault(x => x.OrderId == firstOrderId);
+
+ Assert.IsTrue(NHibernateUtil.IsInitialized(order.Shipper));
+ }
}
}
@@ -60,12 +60,6 @@ private static void CopyFromClauseData(FromClauseBase source, FromClauseBase des
destination.ItemType = source.ItemType;
}
- private static void CopyResultOperators(IEnumerable<ResultOperatorBase> resultOperators, QueryModel queryModel)
- {
- foreach (var bodyClause in resultOperators)
- queryModel.ResultOperators.Add(bodyClause);
- }
-
private static void FlattenSubQuery(SubQueryExpression subQueryExpression, FromClauseBase fromClause, QueryModel queryModel, int destinationIndex)
{
if (!CheckFlattenable(subQueryExpression.QueryModel))
@@ -76,14 +70,24 @@ private static void FlattenSubQuery(SubQueryExpression subQueryExpression, FromC
var innerSelectorMapping = new QuerySourceMapping();
innerSelectorMapping.AddMapping(fromClause, subQueryExpression.QueryModel.SelectClause.Selector);
- queryModel.TransformExpressions((ex => ReferenceReplacingExpressionTreeVisitor.ReplaceClauseReferences(ex, innerSelectorMapping, false)));
+ queryModel.TransformExpressions(ex => ReferenceReplacingExpressionTreeVisitor.ReplaceClauseReferences(ex, innerSelectorMapping, false));
InsertBodyClauses(subQueryExpression.QueryModel.BodyClauses, queryModel, destinationIndex);
- CopyResultOperators(subQueryExpression.QueryModel.ResultOperators, queryModel);
+ InsertResultOperators(subQueryExpression.QueryModel.ResultOperators, queryModel);
var innerBodyClauseMapping = new QuerySourceMapping();
innerBodyClauseMapping.AddMapping(mainFromClause, new QuerySourceReferenceExpression(fromClause));
- queryModel.TransformExpressions((ex => ReferenceReplacingExpressionTreeVisitor.ReplaceClauseReferences(ex, innerBodyClauseMapping, false)));
+ queryModel.TransformExpressions(ex => ReferenceReplacingExpressionTreeVisitor.ReplaceClauseReferences(ex, innerBodyClauseMapping, false));
+ }
+
+ private static void InsertResultOperators(IEnumerable<ResultOperatorBase> resultOperators, QueryModel queryModel)
+ {
+ var index = 0;
+ foreach (var bodyClause in resultOperators)
+ {
+ queryModel.ResultOperators.Insert(index, bodyClause);
+ ++index;
+ }
}
private static void InsertBodyClauses(IEnumerable<IBodyClause> bodyClauses, QueryModel queryModel, int destinationIndex)

0 comments on commit 806cf92

Please sign in to comment.