Skip to content

Commit 3db2893

Browse files
committed
Optimize code in ProcessNonAggregatingGroupBy
1 parent a30bfd3 commit 3db2893

File tree

2 files changed

+16
-29
lines changed

2 files changed

+16
-29
lines changed

src/NHibernate/Linq/Visitors/GroupByKeySelectorVisitor.cs

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,6 @@
44

55
namespace NHibernate.Linq.Visitors
66
{
7-
internal class GroupByKeySelectorVisitor : ExpressionTreeVisitor
8-
{
9-
private readonly Expression _parameter;
10-
11-
public GroupByKeySelectorVisitor(Expression parameter)
12-
{
13-
_parameter = parameter;
14-
}
15-
16-
public Expression Visit(Expression expression)
17-
{
18-
return VisitExpression(expression);
19-
}
20-
21-
protected override Expression VisitQuerySourceReferenceExpression(QuerySourceReferenceExpression expression)
22-
{
23-
return _parameter;
24-
}
25-
}
26-
277
internal class GroupByKeySourceFinder : ExpressionTreeVisitor
288
{
299
private Expression _source;

src/NHibernate/Linq/Visitors/ResultOperatorProcessors/ProcessNonAggregatingGroupBy.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Linq.Expressions;
55
using NHibernate.Linq.ResultOperators;
66
using Remotion.Linq.Clauses.Expressions;
7+
using Remotion.Linq.Parsing.ExpressionTreeVisitors;
78

89
namespace NHibernate.Linq.Visitors.ResultOperatorProcessors
910
{
@@ -44,22 +45,28 @@ public void Process(NonAggregatingGroupBy resultOperator, QueryModelVisitor quer
4445
private static LambdaExpression CreateSelector(System.Type sourceType, Expression selector)
4546
{
4647
var parameter = Expression.Parameter(sourceType, "item");
47-
var selectorSource = (Expression) parameter;
48-
49-
if (sourceType != SourceOf(selector))
48+
49+
var querySource = GetQuerySourceReferenceExpression(selector);
50+
51+
Expression selectorBody;
52+
if (sourceType != querySource.Type)
5053
{
54+
//TODO: it looks like some "magic".
5155
var member = sourceType.GetMember(((QuerySourceReferenceExpression) selector).ReferencedQuerySource.ItemName)[0];
5256

53-
selectorSource = Expression.MakeMemberAccess(parameter, member);
57+
selectorBody = Expression.MakeMemberAccess(parameter, member);
5458
}
55-
56-
var keySelector = new GroupByKeySelectorVisitor(selectorSource).Visit(selector);
57-
return Expression.Lambda(keySelector, parameter);
59+
else
60+
{
61+
selectorBody = ReplacingExpressionTreeVisitor.Replace(querySource, parameter, selector);
62+
}
63+
64+
return Expression.Lambda(selectorBody, parameter);
5865
}
5966

60-
private static System.Type SourceOf(Expression keySelector)
67+
private static Expression GetQuerySourceReferenceExpression(Expression keySelector)
6168
{
62-
return new GroupByKeySourceFinder().Visit(keySelector).Type;
69+
return new GroupByKeySourceFinder().Visit(keySelector);
6370
}
6471
}
6572
}

0 commit comments

Comments
 (0)