EntityToExpression.cs
5.47 KB
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
using System;
using System.Linq.Expressions;
using System.Reflection;
namespace Infrastructure
{
public static class EntityToExpression<T>
{
public static Expression<Func<T, bool>> GetExpressions(T t)
{
ParameterExpression u = Expression.Parameter(typeof(T), "u");
Filter filterObj = new Filter();
filterObj.Key = "";
filterObj.Value = "";
// 获得此模型的类型
Type type = typeof(T);
Expression result = Expression.Constant(true);
// 获得此模型的公共属性
PropertyInfo[] propertys = t.GetType().GetProperties();
for (int i = 0; i < propertys.Length; i++)
{
Object value2 = propertys[i].GetValue(t, null);
if (value2 != null && value2.ToString() != "")
{
filterObj.Key = propertys[i].Name.ToString();
if (propertys[i].PropertyType == typeof(DateTime) || propertys[i].PropertyType == typeof(DateTime?))
{
filterObj.Value = ((DateTime)propertys[i].GetValue(t, null)).ToString("yyyy-MM-dd HH:mm:ss.fff");
}
else
{
filterObj.Value = propertys[i].GetValue(t, null).ToString();
}
if (
(propertys[i].PropertyType == typeof(int) || propertys[i].PropertyType == typeof(int?))
|| (propertys[i].PropertyType == typeof(short) || propertys[i].PropertyType == typeof(short?))
|| (propertys[i].PropertyType == typeof(long) || propertys[i].PropertyType == typeof(long?))
|| (propertys[i].PropertyType == typeof(decimal) || propertys[i].PropertyType == typeof(decimal?))
|| (propertys[i].PropertyType == typeof(double) || propertys[i].PropertyType == typeof(double?))
|| (propertys[i].PropertyType == typeof(bool) || propertys[i].PropertyType == typeof(bool?))
)
{
filterObj.Contrast = ConvertOperString("eq");
filterObj.JqContrast = "eq";
if (propertys[i].PropertyType == typeof(int?) ||
propertys[i].PropertyType == typeof(short?) ||
propertys[i].PropertyType == typeof(long?) ||
propertys[i].PropertyType == typeof(double?) ||
propertys[i].PropertyType == typeof(bool?)
)
{
PropertyInfo property = typeof(T).GetProperty(filterObj.Key);
Expression left = Expression.Property(u, property);
try
{
result = result.AndAlso(Expression.Property(left, "HasValue"));
}
catch (Exception ex)
{
throw ex;
}
}
}
else
{
filterObj.Contrast = ConvertOperString("cn");
filterObj.JqContrast = "cn";
}
result = result.AndAlso(u.GenerateBody<T>(filterObj));
}
}
Expression<Func<T, bool>> expression = u.GenerateTypeLambda<T>(result);
return expression;
}
private static string ConvertOperString(string _searchOper)
{
string sReturn = _searchOper;
switch (_searchOper)
{
case "eq"://等于
sReturn = "==";
break;
case "ne"://不等
sReturn = "!=";
break;
case "lt"://小于
sReturn = "<";
break;
case "le"://小于等于
sReturn = "<=";
break;
case "gt"://大于
sReturn = ">";
break;
case "ge"://大于等于
sReturn = ">=";
break;
case "bw"://开始于
sReturn = "begin with";
break;
case "bn"://不开始于
sReturn = "not begin with";
break;
case "in"://属于
sReturn = "in";
break;
case "ni"://不属于
sReturn = "not in";
break;
case "ew"://结束于
sReturn = "end with";
break;
case "en"://不结束于
sReturn = "not end with";
break;
case "cn"://包含
sReturn = "like";
break;
case "nc"://不包含
sReturn = "not like";
break;
case "nu"://为空
sReturn = "null";
break;
case "nn"://不为空
sReturn = "not null";
break;
}
return sReturn;
}
}
}