Skip to content

Commit 48c7b2d

Browse files
authored
Merge pull request #4 from StefH/patch-1
Refactor and move some classes
2 parents 435af17 + 0f98aff commit 48c7b2d

File tree

6 files changed

+191
-132
lines changed

6 files changed

+191
-132
lines changed

src-console/ConsoleApp_net452_EF6/Program.cs

+13-9
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@ class Program
1111
{
1212
static void Main(string[] args)
1313
{
14-
var all = new
15-
{
16-
test1 = new List<int> { 1, 2, 3 }.ToDynamicList(typeof(int)),
17-
test2 = new List<dynamic> { 4, 5, 6 }.ToDynamicList(typeof(int)),
18-
test3 = new List<object> { 7, 8, 9 }.ToDynamicList(typeof(int))
19-
};
20-
21-
Console.WriteLine("all {0}", JsonConvert.SerializeObject(all, Formatting.Indented));
2214
using (var context = new KendoGridDbContext())
2315
{
16+
var found1 = context.Employees.FirstOrDefault($"EmployeeNumber > 1000");
17+
Console.WriteLine($"found1 : {found1.Id} - {found1.EmployeeNumber}");
18+
19+
var found2 = context.Employees.FirstOrDefault($"EmployeeNumber > @0", 1001);
20+
Console.WriteLine($"found2 : {found2.Id} - {found2.EmployeeNumber}");
21+
22+
int em = 1002;
23+
var found3 = context.Employees.FirstOrDefault($"EmployeeNumber > @0", em);
24+
Console.WriteLine($"found3 : {found3.Id} - {found3.EmployeeNumber}");
25+
26+
return;
27+
2428
string search = "2";
2529
var expected = context.Employees.Where(e => System.Data.Entity.SqlServer.SqlFunctions.StringConvert((double)e.EmployeeNumber).Contains(search)).ToArray();
2630
foreach (var emp in expected)
@@ -34,7 +38,7 @@ static void Main(string[] args)
3438
Console.WriteLine($"DynamicLinq : {emp.Id} - {emp.EmployeeNumber}");
3539
}
3640

37-
Console.WriteLine(new String('-', 80));
41+
Console.WriteLine(new string('-', 80));
3842
int x = 1002;
3943
int seven = 7;
4044
var testNonOptimize = context.Employees.Where(e => e.EmployeeNumber > 1000 && e.EmployeeNumber < x && 7 == seven);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using System.Linq.Expressions;
2+
using System.Reflection;
3+
4+
namespace System.Linq.Dynamic.Core.Parser
5+
{
6+
/// <summary>
7+
/// Based on gblog by graeme-hill. https://github.com/graeme-hill/gblog/blob/master/source_content/articles/2014.139_entity-framework-dynamic-queries-and-parameterization.mkd
8+
/// </summary>
9+
internal static class ConstantExpressionWrapper
10+
{
11+
public static void Wrap(ref Expression expression)
12+
{
13+
if (expression is ConstantExpression)
14+
{
15+
var constantExpression = expression as ConstantExpression;
16+
17+
if (constantExpression.Type == typeof(bool))
18+
{
19+
expression = WrappedConstant((bool)constantExpression.Value);
20+
}
21+
else if (constantExpression.Type == typeof(string))
22+
{
23+
expression = WrappedConstant((string)constantExpression.Value);
24+
}
25+
else if (constantExpression.Type == typeof(float))
26+
{
27+
expression = WrappedConstant((float)constantExpression.Value);
28+
}
29+
else if (constantExpression.Type == typeof(decimal))
30+
{
31+
expression = WrappedConstant((decimal)constantExpression.Value);
32+
}
33+
else if (constantExpression.Type == typeof(double))
34+
{
35+
expression = WrappedConstant((double)constantExpression.Value);
36+
}
37+
else if (constantExpression.Type == typeof(long))
38+
{
39+
expression = WrappedConstant((long)constantExpression.Value);
40+
}
41+
else if (constantExpression.Type == typeof(ulong))
42+
{
43+
expression = WrappedConstant((ulong)constantExpression.Value);
44+
}
45+
else if (constantExpression.Type == typeof(int))
46+
{
47+
expression = WrappedConstant((int)constantExpression.Value);
48+
}
49+
else if (constantExpression.Type == typeof(uint))
50+
{
51+
expression = WrappedConstant((uint)constantExpression.Value);
52+
}
53+
else if (constantExpression.Type == typeof(short))
54+
{
55+
expression = WrappedConstant((short)constantExpression.Value);
56+
}
57+
else if (constantExpression.Type == typeof(ushort))
58+
{
59+
expression = WrappedConstant((ushort)constantExpression.Value);
60+
}
61+
else if (constantExpression.Type == typeof(Guid))
62+
{
63+
expression = WrappedConstant((Guid)constantExpression.Value);
64+
}
65+
else if (constantExpression.Type == typeof(DateTime))
66+
{
67+
expression = WrappedConstant((DateTime)constantExpression.Value);
68+
}
69+
else if (constantExpression.Type == typeof(DateTimeOffset))
70+
{
71+
expression = WrappedConstant((DateTimeOffset)constantExpression.Value);
72+
}
73+
else if (constantExpression.Type == typeof(TimeSpan))
74+
{
75+
expression = WrappedConstant((TimeSpan)constantExpression.Value);
76+
}
77+
78+
return;
79+
}
80+
81+
if (expression is NewExpression)
82+
{
83+
var newExpression = expression as NewExpression;
84+
85+
if (newExpression.Type == typeof(Guid))
86+
{
87+
expression = WrappedConstant(Expression.Lambda<Func<Guid>>(newExpression).Compile()());
88+
}
89+
else if (newExpression.Type == typeof(DateTime))
90+
{
91+
expression = WrappedConstant(Expression.Lambda<Func<DateTime>>(newExpression).Compile()());
92+
}
93+
else if (newExpression.Type == typeof(DateTimeOffset))
94+
{
95+
expression = WrappedConstant(Expression.Lambda<Func<DateTimeOffset>>(newExpression).Compile()());
96+
}
97+
else if (newExpression.Type == typeof(TimeSpan))
98+
{
99+
expression = WrappedConstant(Expression.Lambda<Func<TimeSpan>>(newExpression).Compile()());
100+
}
101+
}
102+
}
103+
104+
private static MemberExpression WrappedConstant<TValue>(TValue value)
105+
{
106+
var wrapper = new WrappedValue<TValue>(value);
107+
108+
return Expression.Property(Expression.Constant(wrapper), typeof(WrappedValue<TValue>).GetProperty("Value"));
109+
}
110+
}
111+
}

src/System.Linq.Dynamic.Core/Parser/Constants.cs

-9
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,4 @@ internal static class Constants
88
public static readonly Expression FalseLiteral = Expression.Constant(false);
99
public static readonly Expression NullLiteral = Expression.Constant(null);
1010
}
11-
12-
public class WrappedObj<TValue>
13-
{
14-
public TValue Value { get; set; }
15-
public WrappedObj(TValue value)
16-
{
17-
this.Value = value;
18-
}
19-
}
2011
}

src/System.Linq.Dynamic.Core/Parser/ExpressionHelper.cs

+20-80
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,20 @@ public static Expression GenerateSubtract(Expression left, Expression right)
6868
public static Expression GenerateEqual(Expression left, Expression right)
6969
{
7070
OptimizeForEqualityIfPossible(ref left, ref right);
71-
72-
WrapConstantExpression(ref left);
73-
WrapConstantExpression(ref right);
74-
71+
72+
ConstantExpressionWrapper.Wrap(ref left);
73+
ConstantExpressionWrapper.Wrap(ref right);
74+
7575
return Expression.Equal(left, right);
7676
}
7777

7878
public static Expression GenerateNotEqual(Expression left, Expression right)
7979
{
8080
OptimizeForEqualityIfPossible(ref left, ref right);
81-
82-
WrapConstantExpression(ref left);
83-
WrapConstantExpression(ref right);
84-
81+
82+
ConstantExpressionWrapper.Wrap(ref left);
83+
ConstantExpressionWrapper.Wrap(ref right);
84+
8585
return Expression.NotEqual(left, right);
8686
}
8787

@@ -98,9 +98,9 @@ public static Expression GenerateGreaterThan(Expression left, Expression right)
9898
var rightPart = right.Type.GetTypeInfo().IsEnum ? Expression.Convert(right, Enum.GetUnderlyingType(right.Type)) : right;
9999
return Expression.GreaterThan(leftPart, rightPart);
100100
}
101-
102-
WrapConstantExpression(ref left);
103-
WrapConstantExpression(ref right);
101+
102+
ConstantExpressionWrapper.Wrap(ref left);
103+
ConstantExpressionWrapper.Wrap(ref right);
104104

105105
return Expression.GreaterThan(left, right);
106106
}
@@ -117,9 +117,9 @@ public static Expression GenerateGreaterThanEqual(Expression left, Expression ri
117117
return Expression.GreaterThanOrEqual(left.Type.GetTypeInfo().IsEnum ? Expression.Convert(left, Enum.GetUnderlyingType(left.Type)) : left,
118118
right.Type.GetTypeInfo().IsEnum ? Expression.Convert(right, Enum.GetUnderlyingType(right.Type)) : right);
119119
}
120-
121-
WrapConstantExpression(ref left);
122-
WrapConstantExpression(ref right);
120+
121+
ConstantExpressionWrapper.Wrap(ref left);
122+
ConstantExpressionWrapper.Wrap(ref right);
123123

124124
return Expression.GreaterThanOrEqual(left, right);
125125
}
@@ -137,8 +137,8 @@ public static Expression GenerateLessThan(Expression left, Expression right)
137137
right.Type.GetTypeInfo().IsEnum ? Expression.Convert(right, Enum.GetUnderlyingType(right.Type)) : right);
138138
}
139139

140-
WrapConstantExpression(ref left);
141-
WrapConstantExpression(ref right);
140+
ConstantExpressionWrapper.Wrap(ref left);
141+
ConstantExpressionWrapper.Wrap(ref right);
142142

143143
return Expression.LessThan(left, right);
144144
}
@@ -156,8 +156,8 @@ public static Expression GenerateLessThanEqual(Expression left, Expression right
156156
right.Type.GetTypeInfo().IsEnum ? Expression.Convert(right, Enum.GetUnderlyingType(right.Type)) : right);
157157
}
158158

159-
WrapConstantExpression(ref left);
160-
WrapConstantExpression(ref right);
159+
ConstantExpressionWrapper.Wrap(ref left);
160+
ConstantExpressionWrapper.Wrap(ref right);
161161

162162
return Expression.LessThanOrEqual(left, right);
163163
}
@@ -208,10 +208,10 @@ public static Expression OptimizeStringForEqualityIfPossible(string text, Type t
208208

209209
static MethodInfo GetStaticMethod(string methodName, Expression left, Expression right)
210210
{
211-
var methodInfo = left.Type.GetMethod(methodName, new[] {left.Type, right.Type});
211+
var methodInfo = left.Type.GetMethod(methodName, new[] { left.Type, right.Type });
212212
if (methodInfo == null)
213213
{
214-
methodInfo = right.Type.GetMethod(methodName, new[] {left.Type, right.Type});
214+
methodInfo = right.Type.GetMethod(methodName, new[] { left.Type, right.Type });
215215
}
216216

217217
return methodInfo;
@@ -221,65 +221,5 @@ static Expression GenerateStaticMethodCall(string methodName, Expression left, E
221221
{
222222
return Expression.Call(null, GetStaticMethod(methodName, left, right), new[] { left, right });
223223
}
224-
225-
static void WrapConstantExpression(ref Expression expression)
226-
{
227-
if (!(expression is ConstantExpression || expression is NewExpression)) return;
228-
229-
if (expression is ConstantExpression)
230-
{
231-
ConstantExpression constantExpression = expression as ConstantExpression;
232-
233-
if (constantExpression.Type == typeof(string))
234-
{
235-
expression = WrappedConstant((string)constantExpression.Value);
236-
}
237-
else if (constantExpression.Type == typeof(long))
238-
{
239-
expression = WrappedConstant((Int64)constantExpression.Value);
240-
}
241-
else if (constantExpression.Type == typeof(int))
242-
{
243-
expression = WrappedConstant((Int32)constantExpression.Value);
244-
}
245-
else if (constantExpression.Type == typeof(short))
246-
{
247-
expression = WrappedConstant((Int16)constantExpression.Value);
248-
}
249-
else if (constantExpression.Type == typeof(Guid))
250-
{
251-
expression = WrappedConstant((Guid)constantExpression.Value);
252-
}
253-
else if (constantExpression.Type == typeof(DateTime))
254-
{
255-
expression = WrappedConstant((DateTime)constantExpression.Value);
256-
}
257-
}
258-
else
259-
{
260-
NewExpression newExpression = expression as NewExpression;
261-
262-
if (newExpression.Type == typeof(Guid))
263-
{
264-
expression = WrappedConstant((Expression.Lambda<Func<Guid>>(newExpression).Compile())());
265-
}
266-
else if (newExpression.Type == typeof(DateTime))
267-
{
268-
expression = WrappedConstant((Expression.Lambda<Func<DateTime>>(newExpression).Compile())());
269-
}
270-
}
271-
}
272-
273-
/// <summary>
274-
/// Based on gblog by graeme-hill. https://github.com/graeme-hill/gblog/blob/master/source_content/articles/2014.139_entity-framework-dynamic-queries-and-parameterization.mkd
275-
/// </summary>
276-
private static MemberExpression WrappedConstant<TValue>(TValue value)
277-
{
278-
var wrapper = new WrappedObj<TValue>(value);
279-
return Expression.Property(
280-
Expression.Constant(wrapper),
281-
typeof(WrappedObj<TValue>).GetProperty("Value"));
282-
}
283-
284224
}
285225
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace System.Linq.Dynamic.Core.Parser
2+
{
3+
public class WrappedValue<TValue>
4+
{
5+
public TValue Value { get; private set; }
6+
7+
public WrappedValue(TValue value)
8+
{
9+
Value = value;
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)