0
|
1 using System;
|
|
2 using System.Linq.Expressions;
|
|
3
|
|
4 namespace BLToolkit.Data.Linq.Builder
|
|
5 {
|
|
6 using BLToolkit.Linq;
|
|
7
|
|
8 class WhereBuilder : MethodCallBuilder
|
|
9 {
|
|
10 protected override bool CanBuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
|
|
11 {
|
|
12 return methodCall.IsQueryable("Where");
|
|
13 }
|
|
14
|
|
15 protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
|
|
16 {
|
|
17 var sequence = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0]));
|
|
18 var condition = (LambdaExpression)methodCall.Arguments[1].Unwrap();
|
|
19 var result = builder.BuildWhere(buildInfo.Parent, sequence, condition, true);
|
|
20
|
|
21 result.SetAlias(condition.Parameters[0].Name);
|
|
22
|
|
23 return result;
|
|
24 }
|
|
25
|
|
26 protected override SequenceConvertInfo Convert(
|
|
27 ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo, ParameterExpression param)
|
|
28 {
|
|
29 var predicate = (LambdaExpression)methodCall.Arguments[1].Unwrap();
|
|
30 var info = builder.ConvertSequence(new BuildInfo(buildInfo, methodCall.Arguments[0]), predicate.Parameters[0]);
|
|
31
|
|
32 if (info != null)
|
|
33 {
|
|
34 info.Expression = methodCall.Convert(ex => ConvertMethod(methodCall, 0, info, predicate.Parameters[0], ex));
|
|
35
|
|
36 if (param != null)
|
|
37 {
|
|
38 if (param.Type != info.Parameter.Type)
|
|
39 param = Expression.Parameter(info.Parameter.Type, param.Name);
|
|
40
|
|
41 if (info.ExpressionsToReplace != null)
|
|
42 foreach (var path in info.ExpressionsToReplace)
|
|
43 {
|
|
44 path.Path = path.Path.Convert(e => e == info.Parameter ? param : e);
|
|
45 path.Expr = path.Expr.Convert(e => e == info.Parameter ? param : e);
|
|
46 }
|
|
47 }
|
|
48
|
|
49 info.Parameter = param;
|
|
50
|
|
51 return info;
|
|
52 }
|
|
53
|
|
54 return null;
|
|
55 }
|
|
56 }
|
|
57 }
|