Blame view

sys/Hh.Mes.Common/Infrastructure/EntityToExpression.cs 5.44 KB
赖素文 authored
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
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;

namespace Hh.Mes.Common.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++)
            {
                dynamic value2 = propertys[i].GetValue(t, null);
-  
唐召明 authored
26
                if (value2 != null && value2.ToString() != "")
赖素文 authored
27
                {
-  
唐召明 authored
28
                    if (propertys[i].PropertyType.Name.Equals("List`1") || propertys[i].PropertyType.Name.Equals("Guid"))
赖素文 authored
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
                    {
                        break;
                    }
                    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);
-  
唐召明 authored
62
                            result = result.AndAlso(Expression.Property(left, "HasValue"));
赖素文 authored
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
                        }
                    }
                    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;
        }
    }

}