0
|
1 using System;
|
|
2 using System.Data;
|
|
3 using System.Linq.Expressions;
|
|
4
|
|
5 namespace BLToolkit.Data.Linq.Builder
|
|
6 {
|
|
7 using BLToolkit.Linq;
|
|
8
|
|
9 class CastBuilder : MethodCallBuilder
|
|
10 {
|
|
11 protected override bool CanBuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
|
|
12 {
|
|
13 return methodCall.IsQueryable("Cast");
|
|
14 }
|
|
15
|
|
16 protected override IBuildContext BuildMethodCall(ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo)
|
|
17 {
|
|
18 var sequence = builder.BuildSequence(new BuildInfo(buildInfo, methodCall.Arguments[0]));
|
|
19
|
|
20 return new CastContext(sequence, methodCall);
|
|
21 }
|
|
22
|
|
23 protected override SequenceConvertInfo Convert(
|
|
24 ExpressionBuilder builder, MethodCallExpression methodCall, BuildInfo buildInfo, ParameterExpression param)
|
|
25 {
|
|
26 return null;
|
|
27 }
|
|
28
|
|
29 class CastContext : PassThroughContext
|
|
30 {
|
|
31 public CastContext(IBuildContext context, MethodCallExpression methodCall)
|
|
32 : base(context)
|
|
33 {
|
|
34 _methodCall = methodCall;
|
|
35 }
|
|
36
|
|
37 private readonly MethodCallExpression _methodCall;
|
|
38
|
|
39 public override void BuildQuery<T>(Query<T> query, ParameterExpression queryParameter)
|
|
40 {
|
|
41 var expr = BuildExpression(null, 0);
|
|
42 var mapper = Builder.BuildMapper<T>(expr);
|
|
43
|
|
44 query.SetQuery(mapper.Compile());
|
|
45 }
|
|
46
|
|
47 public override Expression BuildExpression(Expression expression, int level)
|
|
48 {
|
|
49 var expr = base.BuildExpression(expression, level);
|
|
50 var type = _methodCall.Method.GetGenericArguments()[0];
|
|
51
|
|
52 if (expr.Type != type)
|
|
53 expr = Expression.Convert(expr, type);
|
|
54
|
|
55 return expr;
|
|
56 }
|
|
57 }
|
|
58 }
|
|
59 }
|