表达式树的说明与运用

说明: 在我们日常代码开发中很多的地方都用到了Lambda表达式进行过滤操作,我们很多优秀的ORM也是使用表达式来进行数据的查询。但是对于一些复杂的过 滤单纯的使用Lambda已经不能够解决问题了那么就需要表达式树来进行条件的一个拼接。

下面介绍一个本人写的一个工具类有助于项目中更好的使用:

复制代码
1 public static class ExpressionTreeTools
2 {
3 ///
4 /// 相当于&&操作
5 /// ——just0ne
6 ///
7 /// 已生成的过滤条件
8 /// 未生成的过滤条件
9 /// 新的过滤
10 public static Expression And(this Expression thisFilter, Expression otherFilter)
11 {
12 return Expression.AndAlso(thisFilter, otherFilter);
13 }
14 ///
15 /// 相当于||操作
16 /// ——just0ne
17 ///
18 /// 已生成的过滤条件
19 /// 未生成的过滤条件
20 /// 新的过滤
21 public static Expression Or(this Expression thisFilter, Expression otherFilter)
22 {
23 return Expression.OrElse(thisFilter, otherFilter);
24 }
25 ///
26 /// 相当于==操作
27 /// ——just0ne
28 ///
29 /// 查询对象
30 /// 属性名称
31 /// 属性值
32 /// 新的过滤
33 public static Expression GotoEqual(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
34 {
35 return Expression.Equal(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue));
36 }
37 ///
38 /// 相当于>=操作
39 /// ——just0ne
40 ///
41 /// 查询对象
42 /// 属性名称
43 /// 属性值
44 /// 新的过滤
45 public static Expression GotoGreaterThanOrEqual(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
46 {
47 //大于或等于
48 return Expression.GreaterThanOrEqual(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(T)));
49 }
50 ///
51 /// 相当于小于等于操作
52 /// ——just0ne
53 ///
54 /// 查询对象
55 /// 属性名称
56 /// 属性值
57 /// 新的过滤
58 public static Expression GotoLessThanOrEqual(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
59 {
60 //小于或等于
61 return Expression.LessThanOrEqual(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(T)));
62 }
63 ///
64 /// 相当于>操作
65 /// ——just0ne
66 ///
67 /// 查询对象
68 /// 属性名称
69 /// 属性值
70 /// 新的过滤
71 public static Expression GotoGreaterThan(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
72 {
73 //大于
74 return Expression.GreaterThan(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(T)));
75 }
76 ///
77 /// 相当于小于操作
78 /// ——just0ne
79 ///
80 /// 查询对象
81 /// 属性名称
82 /// 属性值
83 /// 新的过滤
84 public static Expression GotoLessThan(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
85 {
86 //小于
87 return Expression.LessThan(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(T)));
88 }
89 ///
90 /// 相当于>=操作
91 /// ——just0ne
92 ///
93 /// 查询对象
94 /// 属性名称
95 /// 属性值
96 /// 新的过滤
97 public static Expression GotoGreaterThanOrEqualByDateTime(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
98 {
99 //大于或等于
100 return Expression.GreaterThanOrEqual(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(DateTime?)));
101 }
102 ///
103 /// 相当于小于或等于操作
104 /// ——just0ne
105 ///
106 /// 查询对象
107 /// 属性名称
108 /// 属性值
109 /// 新的过滤
110 public static Expression GotoLessThanOrEqualByDateTime(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
111 {
112 //小于或等于
113 return Expression.LessThanOrEqual(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(DateTime?)));
114 }
115 ///
116 /// 相当于>操作
117 /// ——just0ne
118 ///
119 /// 查询对象
120 /// 属性名称
121 /// 属性值
122 /// 新的过滤
123 public static Expression GotoGreaterThanByDateTime(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
124 {
125 //大于
126 return Expression.GreaterThan(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(DateTime?)));
127 }
128 ///
129 /// 相当于小于操作
130 /// —一just0ne
131 ///
132 /// 查询对象
133 /// 属性名称
134 /// 属性值
135 /// 新的过滤
136 public static Expression GotoLessThanByDateTime(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
137 {
138 //小于
139 return Expression.LessThan(Expression.Property(thisParameterExpression, propertieName), Expression.Constant(propertieValue, typeof(DateTime?)));
140 }
141
142 ///
143 /// 一一just0ne
144 /// 包含操作 相当余 a=> arr.Contains(a.ID)
145 /// 如果arr中数据量过大则不适用linq
146 ///
147 /// 查询对象
148 /// 属性名称
149 /// 属性值
150 /// 新的过滤
151 public static Expression ContainsOperations(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
152 {
153 MethodInfo method = null;
154 MemberExpression member = Expression.Property(thisParameterExpression, propertieName);
155 var containsMethods = typeof(Enumerable).GetMethods(BindingFlags.Static | BindingFlags.Public).Where(m => m.Name == “Contains”);
156 foreach (var m in containsMethods)
157 {
158 if (m.GetParameters().Count() == 2)
159 {
160 method = m;
161 break;
162 }
163 }
164 method = method.MakeGenericMethod(member.Type);
165 var exprContains = Expression.Call(method, new Expression[] { Expression.Constant(propertieValue), member });
166 return exprContains;
167 }
168
169 ///
170 /// 一一just0ne
171 /// 包含操作 相当于 a=>a.ID.Contains(key)
172 ///
173 /// 查询对象
174 /// 属性名称
175 /// 属性值
176 /// 新的过滤
177 public static Expression Contains(this ParameterExpression thisParameterExpression, string propertieName, object propertieValue)
178 {
179 var propertyExp = Expression.Property(thisParameterExpression, propertieName);
180 MethodInfo method = typeof(string).GetMethod(“Contains”, new[] { typeof(string) });
181 var someValue = Expression.Constant(propertieValue, typeof(string));
182 var containsMethodExp = Expression.Call(propertyExp, method, someValue);
183 return containsMethodExp;
184 }
185
186 }
复制代码
以上就是一些基本的拼接都有了接下来是如何进行使用 我们还是贴上代码进行说明

复制代码
class Program
{
static void Main(string[] args)
{
string[] nameArr = new string[] { “just0ne”, “kphui”, “point” };
var userDataList = GetUserDataList().AsQueryable();
//初始化
var parameterExpression = Expression.Parameter(typeof(UserData));
var filter = (Expression)Expression.Constant(true);
//寻找年岁大于10
filter = filter.And(parameterExpression.GotoGreaterThan(“Age”, 10));
string key = Console.ReadLine();
if (!String.IsNullOrEmpty(key))
{
var keyFilter = (Expression)Expression.Constant(false);
keyFilter = keyFilter.Or(parameterExpression.Contains(“Name”, key));
keyFilter = keyFilter.Or(parameterExpression.Contains(“Phone”, key));
filter = filter.And(keyFilter);
}
filter = filter.And(parameterExpression.ContainsOperations(“Name”, nameArr));
var lamadaFilter = Expression.Lambda<Func<UserData, bool>>(filter, parameterExpression);
var userDatas = userDataList.Where(lamadaFilter).ToList();
Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(userDatas));
}

    public static List<UserData> GetUserDataList()
    {
        return new List<UserData>()
        {
            new UserData(){ Age=18, Id=1, Name="just0ne", Phone="13856****26" },
            new UserData(){ Age=22, Id=2, Name="point", Phone="17521****52" },
            new UserData(){ Age=21, Id=3, Name="geekdog", Phone="15562****36" },
            new UserData(){ Age=14, Id=4, Name="kphui" , Phone="13577****26"},
            new UserData(){ Age=13, Id=5, Name="lg" , Phone="13456****26"},
            new UserData(){ Age=16, Id=6, Name="ming", Phone="13356****26" },
            new UserData(){ Age=18, Id=7, Name="tencent", Phone="13256****26" },
            new UserData(){ Age=10, Id=8, Name="justin" , Phone="13156****26"},
            new UserData(){ Age=31, Id=9, Name="hujw", Phone="13823****26" },
            new UserData(){ Age=27, Id=10, Name="lqm" , Phone="13876****26"},
            new UserData(){ Age=26, Id=11, Name="jiujiu" , Phone="13846****26"},
        };
    }
}

public class UserData
{
    public int Id { get; set; }

    public string Name { get; set; }

    public string Phone { get; set; }

    public string Email { get; set; }

    public int Age { get; set; }
}

复制代码
东莞网站建设www.zg886.cn

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值