-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectiveQuery.cs
183 lines (154 loc) · 6.88 KB
/
SelectiveQuery.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using static EFDynamicLinqQuery.RunTimeTypeBuilder;
namespace EFDynamicLinqQuery
{
public static class SelectiveQuery
{
public static ICollection<object> SelectPropertiesDynamic<T>(
this IQueryable<T> source,
IEnumerable<string> selectedProperties) where T : class
{
// Take properties from the mapped entitiy that match selected properties
IDictionary<string, PropertyInfo> sourceProperties =
GetTypeProperties<T>(selectedProperties);
// Construct runtime type by given property configuration
Type runtimeType = RuntimeTypeBuilder.GetRuntimeType(sourceProperties);
Type sourceType = typeof(T);
// Create instance of source parameter
ParameterExpression sourceParameter = Expression.Parameter(sourceType, "t");
// Take fields from generated runtime type
FieldInfo[] runtimeTypeFields = runtimeType.GetFields();
// Generate bindings from source type to runtime type
IEnumerable<MemberBinding> bindingsToRuntimeType = runtimeTypeFields
.Select(field => Expression.Bind(
field,
Expression.Property(
sourceParameter,
sourceProperties[field.Name]
)
));
// Generate projection trom T to runtimeType and cast as IQueryable<object>
IQueryable<object> runtimeTypeSelectExpressionQuery
= GetTypeSelectExpressionQuery<object>(
sourceType,
runtimeType,
bindingsToRuntimeType,
source,
sourceParameter
);
// Get result from database
List<object> listOfObjects = runtimeTypeSelectExpressionQuery.ToList();
return listOfObjects;
}
public static ICollection<T> SelectProperties<T>(
this IQueryable<T> source,
IEnumerable<string> selectedProperties) where T : class
{
// Take properties from the mapped entitiy that match selected properties
IDictionary<string, PropertyInfo> sourceProperties =
GetTypeProperties<T>(selectedProperties);
// Construct runtime type by given property configuration
Type runtimeType = RuntimeTypeBuilder.GetRuntimeType(sourceProperties);
Type sourceType = typeof(T);
// Create instance of source parameter
ParameterExpression sourceParameter = Expression.Parameter(sourceType, "t");
// Take fields from generated runtime type
FieldInfo[] runtimeTypeFields = runtimeType.GetFields();
// Generate bindings from source type to runtime type
IEnumerable<MemberBinding> bindingsToRuntimeType = runtimeTypeFields
.Select(field => Expression.Bind(
field,
Expression.Property(
sourceParameter,
sourceProperties[field.Name]
)
));
// Generate projection trom T to runtimeType and cast as IQueryable<object>
IQueryable<object> runtimeTypeSelectExpressionQuery
= GetTypeSelectExpressionQuery<object>(
sourceType,
runtimeType,
bindingsToRuntimeType,
source,
sourceParameter
);
// Get result from database
List<object> listOfObjects = runtimeTypeSelectExpressionQuery.ToList();
MethodInfo castMethod = typeof(Queryable)
.GetMethod("Cast", BindingFlags.Public | BindingFlags.Static)
.MakeGenericMethod(runtimeType);
// Cast list<objects> to IQueryable<runtimeType>
IQueryable castedSource = castMethod.Invoke(
null,
new Object[] { listOfObjects.AsQueryable() }
) as IQueryable;
// Create instance of runtime type parameter
ParameterExpression runtimeParameter = Expression.Parameter(runtimeType, "p");
IDictionary<string, FieldInfo> dynamicTypeFieldsDict =
runtimeTypeFields.ToDictionary(f => f.Name, f => f);
// Generate bindings from runtime type to source type
IEnumerable<MemberBinding> bindingsToTargetType = sourceProperties.Values
.Select(property => Expression.Bind(
property,
Expression.Field(
runtimeParameter,
dynamicTypeFieldsDict[property.Name]
)
));
// Generate projection trom runtimeType to T and cast as IQueryable<object>
IQueryable<T> targetTypeSelectExpressionQuery
= GetTypeSelectExpressionQuery<T>(
runtimeType,
sourceType,
bindingsToTargetType,
castedSource,
runtimeParameter
);
// Return list of T
return targetTypeSelectExpressionQuery.ToList();
}
private static IQueryable<TT> GetTypeSelectExpressionQuery<TT>(
Type sourceType,
Type targetType,
IEnumerable<MemberBinding> binding,
IQueryable source,
ParameterExpression sourceParameter)
{
LambdaExpression typeSelector =
Expression.Lambda(
Expression.MemberInit(
Expression.New(
targetType.GetConstructor(Type.EmptyTypes)
),
binding
),
sourceParameter
);
MethodCallExpression typeSelectExpression =
Expression.Call(
typeof(Queryable),
"Select",
new[] { sourceType, targetType },
Expression.Constant(source),
typeSelector
);
return Expression.Lambda(typeSelectExpression)
.Compile()
.DynamicInvoke() as IQueryable<TT>;
}
private static IDictionary<string, PropertyInfo> GetTypeProperties<T>(
IEnumerable<string> selectedProperties) where T : class
{
var existedProperties = typeof(T)
.GetProperties()
.ToDictionary(p => p.Name);
return selectedProperties
.Where(existedProperties.ContainsKey)
.ToDictionary(p => p, p => existedProperties[p]);
}
}
}