zs
2025-05-14 800a94c4088b5ad82bdb433a9199dc2183d86959
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
using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;
 
namespace CmsQueryExtensions.Extension
{
    public class WhereHelper<T>
 
    //where T : class
 
    {
 
        public ParameterExpression param;
 
        public BinaryExpression filter;
 
        private MemberExpression common_left;
 
        private UnaryExpression common_right;
        //private ConstantExpression common_right;
 
        public WhereHelper()
 
        {
 
            param = Expression.Parameter(typeof(T), "c");//构建 c=> 结构
 
            //1==1
            Expression left = Expression.Constant(1);
            filter = Expression.Equal(left, left);//构建出 c=> 1=1
 
        }
 
        public Expression<Func<T, bool>> GetExpression()
 
        {
 
            return Expression.Lambda<Func<T, bool>>(filter, param);
 
        }
 
        private void CommonLeftRight(string propertyName, object value)
        {
 
            common_left = Expression.Property(param, typeof(T).GetProperty(propertyName));//构建构建c.{propertyName}的结构
 
            var member = Expression.Property(param, propertyName);
            var propertyType = ((PropertyInfo)member.Member).PropertyType;
            var converter = TypeDescriptor.GetConverter(propertyType); // 1
 
            if (!converter.CanConvertFrom(typeof(string))) // 2
                throw new NotSupportedException();
 
            var propertyValue = converter.ConvertFromInvariantString(value.ToString()); // 3
            var constant = Expression.Constant(propertyValue);
            //common_right = constant; // 4
            common_right = Expression.Convert(constant, propertyType); // 4 //这里必须转换,否则如果字段可为NULL,就会报错
            // common_right = Expression.Constant(value, value.GetType());//构建一个常量,值是  value
        }
 
        /// <summary>
        /// 等于 =
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public void Equal(string propertyName, object value)
        {
            CommonLeftRight(propertyName, value);
 
            Expression result = Expression.Equal(common_left, common_right);
 
            filter = Expression.AndAlso(filter, result);
        }
 
        /// <summary>
        /// 不等于 <>
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public void NotEqual(string propertyName, object value)
        {
 
            CommonLeftRight(propertyName, value);
 
            Expression result = Expression.NotEqual(common_left, common_right);
 
            filter = Expression.AndAlso(filter, result);
 
        }
 
        /// <summary>
        /// 大于 >
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public void GreaterThan(string propertyName, object value)
        {
 
            CommonLeftRight(propertyName, value);
 
            Expression result = Expression.GreaterThan(common_left, common_right);
 
            filter = Expression.AndAlso(filter, result);
 
        }
 
        /// <summary>
        /// 大于等于 >=
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public void GreaterThanOrEqual(string propertyName, object value)
        {
 
            CommonLeftRight(propertyName, value);
 
            Expression result = Expression.GreaterThanOrEqual(common_left, common_right);
 
            filter = Expression.AndAlso(filter, result);
 
        }
 
        /// <summary>
        /// 小于 <
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public void LessThan(string propertyName, object value)
        {
 
            CommonLeftRight(propertyName, value);
 
            Expression result = Expression.LessThan(common_left, common_right);
 
            filter = Expression.AndAlso(filter, result);
 
        }
 
        /// <summary>
        /// 小于等于 <=
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public void LessThanOrEqual(string propertyName, object value)
        {
 
            CommonLeftRight(propertyName, value);
 
            Expression result = Expression.LessThanOrEqual(common_left, common_right);
 
            filter = Expression.AndAlso(filter, result);
 
        }
 
        /// <summary>
        /// 包含 LIKE
        /// </summary>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public void Contains(string propertyName, string value)
 
        {
 
            Expression left = Expression.Property(param, typeof(T).GetProperty(propertyName));
 
            Expression right = Expression.Constant(value, value.GetType());
 
            Expression result = Expression.Call(left, typeof(string).GetMethod("Contains", new Type[] { typeof(string) }), right);
 
            filter = Expression.AndAlso(filter, result);
 
        }
 
 
 
    }
}