RSCG- part 15 – Expression Generator

 

 

name Property Expression Generator
nuget

https://www.nuget.org/packages/AOPMethodsCommon/
https://www.nuget.org/packages/AOPMethodsGenerator/

link http://msprogrammer.serviciipeweb.ro/category/roslyn/
author Andrei Ignat

This will generate code to add function to be used with Entity Framework to search for any property of a class
 

The code that you start with is

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
[AutoMethods(template = TemplateMethod.CustomTemplateFile, CustomTemplateFileName = "CreateMetadata.txt")]
 
public partial class Person
 
{
 
    public int ID { get; set; }
 
    public string FirstName { get; set; }
 
    public string LastName { get; set; }
 
    public DateTime? DateOfBirth {get;set;}
 
}

The code that you will use is

01
02
03
04
05
06
07
08
09
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
var queryCnt = Metadata_Person.expr_FirstName_Contains("9");            
 
var pers= await cnt.Person.Where(queryCnt).ToArrayAsync();
 
Console.WriteLine(pers.Length);
 
 
 
 
 
queryCnt = Metadata_Person.expr_LastName_NullOrWhite();
 
pers = await cnt.Person.Where(queryCnt).ToArrayAsync();
 
Console.WriteLine(pers.Length);
 
 
 
 
 
var queryID = Metadata_Person.expr_ID_Equal(7);
 
var pId = await cnt.Person.FirstOrDefaultAsync(queryID);
 
Console.WriteLine(pId.FirstName);
 
 
 
 
 
queryID = Metadata_Person.expr_ID_Contains(7,9);
 
pers = await cnt.Person.Where(queryID).ToArrayAsync();
 
Console.WriteLine(pers.Length);
 
 
 
 
 
var nullBirthDateQuery = Metadata_Person.expr_DateOfBirth_Null();
 
var birthNull = await cnt.Person.Where(nullBirthDateQuery).ToArrayAsync();
 
Console.WriteLine(birthNull.Length);
 
 
 
var query = Metadata_Person.FindEx("ID", SearchCriteria.Equal, 99);
 
pers = await cnt.Person.Where(query).ToArrayAsync();
 
Console.WriteLine(pers.Length);
 
 
 
query = Metadata_Person.FindEx("DateOfBirth", SearchCriteria.FindNull);
 
pers = await cnt.Person.Where(query).ToArrayAsync();
 
Console.WriteLine(pers.Length);

 

The code that is generated is

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
[CompilerGenerated]                                                                                                                                               
 
public partial class Metadata_Person{
 
 
 
     
 
     
 
    //public const string prop_ID = "ID";   
 
    //public static readonly Func<Person,int> func_ID = (it=>it.ID);
 
    //public static readonly Expression<Func<Person,int>> expr_ID = (it=>it.ID);
 
    public static Expression<Func<Person,bool>> expr_ID_Equal(int value)=> (it=>it.ID == value);
 
    public static Expression<Func<Person,bool>> expr_ID_Diff(int value)=> (it=>it.ID != value);
 
    public static Expression<Func<Person,bool>> expr_ID_Contains(params int[] value)=> (it=> value.Contains(it.ID) );
 
     
 
    //int
 
     
 
     
 
     
 
     
 
     
 
         
 
    public static Expression<Func<Person,bool>> expr_ID_Greater(int value)=> (it=>it.ID > value);
 
    public static Expression<Func<Person,bool>> expr_ID_GreaterOrEqual(int value)=> (it=>it.ID >= value);
 
    public static Expression<Func<Person,bool>> expr_ID_Less(int value)=> (it=>it.ID < value);
 
    public static Expression<Func<Person,bool>> expr_ID_LessOrEqual(int value)=> (it=>it.ID <= value);
 
     
 
     
 
             
 
     
 
    //public const string prop_FirstName = "FirstName";   
 
    //public static readonly Func<Person,string> func_FirstName = (it=>it.FirstName);
 
    //public static readonly Expression<Func<Person,string>> expr_FirstName = (it=>it.FirstName);
 
    public static Expression<Func<Person,bool>> expr_FirstName_Equal(string value)=> (it=>it.FirstName == value);
 
    public static Expression<Func<Person,bool>> expr_FirstName_Diff(string value)=> (it=>it.FirstName != value);
 
    public static Expression<Func<Person,bool>> expr_FirstName_Contains(params string[] value)=> (it=> value.Contains(it.FirstName) );
 
     
 
    //string
 
     
 
     
 
         
 
    public static Expression<Func<Person,bool>> expr_FirstName_Null()=> (it=>it.FirstName == null);           
 
     
 
         
 
     
 
 
 
    public static Expression<Func<Person,bool>> expr_FirstName_NullOrWhite()=> (it=>string.IsNullOrWhiteSpace(it.FirstName));
 
 
 
    public static Expression<Func<Person,bool>> expr_FirstName_Ends(string value)=> (it=>it.FirstName.StartsWith (value));
 
    public static Expression<Func<Person,bool>> expr_FirstName_Starts(string value)=> (it=>it.FirstName.EndsWith(value));
 
    public static Expression<Func<Person,bool>> expr_FirstName_Contains(string value)=> (it=>it.FirstName.Contains(value));   
 
     
 
     
 
     
 
     
 
             
 
     
 
    //public const string prop_LastName = "LastName";   
 
    //public static readonly Func<Person,string> func_LastName = (it=>it.LastName);
 
    //public static readonly Expression<Func<Person,string>> expr_LastName = (it=>it.LastName);
 
    public static Expression<Func<Person,bool>> expr_LastName_Equal(string value)=> (it=>it.LastName == value);
 
    public static Expression<Func<Person,bool>> expr_LastName_Diff(string value)=> (it=>it.LastName != value);
 
    public static Expression<Func<Person,bool>> expr_LastName_Contains(params string[] value)=> (it=> value.Contains(it.LastName) );
 
     
 
    //string
 
     
 
     
 
         
 
    public static Expression<Func<Person,bool>> expr_LastName_Null()=> (it=>it.LastName == null);           
 
     
 
         
 
     
 
 
 
    public static Expression<Func<Person,bool>> expr_LastName_NullOrWhite()=> (it=>string.IsNullOrWhiteSpace(it.LastName));
 
 
 
    public static Expression<Func<Person,bool>> expr_LastName_Ends(string value)=> (it=>it.LastName.StartsWith (value));
 
    public static Expression<Func<Person,bool>> expr_LastName_Starts(string value)=> (it=>it.LastName.EndsWith(value));
 
    public static Expression<Func<Person,bool>> expr_LastName_Contains(string value)=> (it=>it.LastName.Contains(value));   
 
     
 
     
 
     
 
     
 
             
 
     
 
    //public const string prop_DateOfBirth = "DateOfBirth";   
 
    //public static readonly Func<Person,System.DateTime?> func_DateOfBirth = (it=>it.DateOfBirth);
 
    //public static readonly Expression<Func<Person,System.DateTime?>> expr_DateOfBirth = (it=>it.DateOfBirth);
 
    public static Expression<Func<Person,bool>> expr_DateOfBirth_Equal(System.DateTime? value)=> (it=>it.DateOfBirth == value);
 
    public static Expression<Func<Person,bool>> expr_DateOfBirth_Diff(System.DateTime? value)=> (it=>it.DateOfBirth != value);
 
    public static Expression<Func<Person,bool>> expr_DateOfBirth_Contains(params System.DateTime?[] value)=> (it=> value.Contains(it.DateOfBirth) );
 
     
 
    //System.DateTime?
 
     
 
     
 
         
 
    public static Expression<Func<Person,bool>> expr_DateOfBirth_Null()=> (it=>it.DateOfBirth == null);           
 
     
 
     
 
     
 
         
 
    public static Expression<Func<Person,bool>> expr_DateOfBirth_Greater(System.DateTime? value)=> (it=>it.DateOfBirth > value);
 
    public static Expression<Func<Person,bool>> expr_DateOfBirth_GreaterOrEqual(System.DateTime? value)=> (it=>it.DateOfBirth >= value);
 
    public static Expression<Func<Person,bool>> expr_DateOfBirth_Less(System.DateTime? value)=> (it=>it.DateOfBirth < value);
 
    public static Expression<Func<Person,bool>> expr_DateOfBirth_LessOrEqual(System.DateTime? value)=> (it=>it.DateOfBirth <= value);
 
     
 
     
 
     
 
 
 
    public static Expression<Func<Person,bool>> FindEx(string nameProp, SearchCriteria search, object value = null)
 
    {
 
         
 
         
 
         
 
        if(string.Compare("ID",nameProp,StringComparison.CurrentCultureIgnoreCase) == 0)
 
        switch(search){
 
            case SearchCriteria.None:
 
                return null;
 
             
 
            case SearchCriteria.Equal:
 
                var orig= (int) value;
 
                return expr_ID_Equal(orig);
 
            default:
 
                throw new ArgumentException("cannot find for ID case  "+search);
 
        }
 
         
 
                     
 
         
 
        if(string.Compare("FirstName",nameProp,StringComparison.CurrentCultureIgnoreCase) == 0)
 
        switch(search){
 
            case SearchCriteria.None:
 
                return null;
 
             
 
            case SearchCriteria.FindNull:
 
                return expr_FirstName_Null();
 
             
 
            case SearchCriteria.Equal:
 
                var orig= (string) value;
 
                return expr_FirstName_Equal(orig);
 
            default:
 
                throw new ArgumentException("cannot find for FirstName case  "+search);
 
        }
 
         
 
                     
 
         
 
        if(string.Compare("LastName",nameProp,StringComparison.CurrentCultureIgnoreCase) == 0)
 
        switch(search){
 
            case SearchCriteria.None:
 
                return null;
 
             
 
            case SearchCriteria.FindNull:
 
                return expr_LastName_Null();
 
             
 
            case SearchCriteria.Equal:
 
                var orig= (string) value;
 
                return expr_LastName_Equal(orig);
 
            default:
 
                throw new ArgumentException("cannot find for LastName case  "+search);
 
        }
 
         
 
                     
 
         
 
        if(string.Compare("DateOfBirth",nameProp,StringComparison.CurrentCultureIgnoreCase) == 0)
 
        switch(search){
 
            case SearchCriteria.None:
 
                return null;
 
             
 
            case SearchCriteria.FindNull:
 
                return expr_DateOfBirth_Null();
 
             
 
            case SearchCriteria.Equal:
 
                var orig= (System.DateTime?) value;
 
                return expr_DateOfBirth_Equal(orig);
 
            default:
 
                throw new ArgumentException("cannot find for DateOfBirth case  "+search);
 
        }
 
         
 
         
 
        throw new ArgumentException("cannot find property  "+nameProp);
 
         
 
    }
 
 
 
}

Example Code: https://github.com/ignatandrei/RSCG_Examples/tree/main/PropertyExpressionGenerator

All RSCG

Roslyn Source Code Generators