@@ -32,9 +32,9 @@ public class NestedDto3
32
32
public int Id { get ; set ; }
33
33
}
34
34
35
- class NetCore21CustomTypeProvider : AbstractDynamicLinqCustomTypeProvider , IDynamicLinkCustomTypeProvider
35
+ class TestCustomTypeProvider : DefaultDynamicLinqCustomTypeProvider , IDynamicLinkCustomTypeProvider
36
36
{
37
- public HashSet < Type > GetCustomTypes ( )
37
+ public new HashSet < Type > GetCustomTypes ( )
38
38
{
39
39
var assemblies = AppDomain . CurrentDomain . GetAssemblies ( ) ;
40
40
@@ -45,68 +45,60 @@ public HashSet<Type> GetCustomTypes()
45
45
46
46
return set ;
47
47
}
48
-
49
- public Type ResolveType ( string typeName )
50
- {
51
- var assemblies = AppDomain . CurrentDomain . GetAssemblies ( ) ;
52
- return ResolveType ( assemblies , typeName ) ;
53
- }
54
48
}
55
49
56
50
static void Main ( string [ ] args )
57
51
{
58
- var q = new [ ] { new NestedDto ( ) , new NestedDto { NestedDto2 = new NestedDto2 { NestedDto3 = new NestedDto3 { Id = 42 } } } } . AsQueryable ( ) ;
52
+ // var q = new[] { new NestedDto(), new NestedDto { NestedDto2 = new NestedDto2 { NestedDto3 = new NestedDto3 { Id = 42 } } } }.AsQueryable();
59
53
60
- var np1 = q . Select ( "np(it.NestedDto2.NestedDto3.Id, 0)" ) ;
61
- var npResult1 = np1 . ToDynamicList < int > ( ) ;
62
- Console . WriteLine ( "npResult1 {0}" , JsonConvert . SerializeObject ( npResult1 , Formatting . Indented ) ) ;
54
+ // var np1 = q.Select("np(it.NestedDto2.NestedDto3.Id, 0)");
55
+ // var npResult1 = np1.ToDynamicList<int>();
56
+ // Console.WriteLine("npResult1 {0}", JsonConvert.SerializeObject(npResult1, Formatting.Indented));
63
57
64
- var np2 = q . Select ( "np(it.NestedDto2.NestedDto3.Id)" ) ;
65
- var npResult2 = np2 . ToDynamicList < int ? > ( ) ;
66
- Console . WriteLine ( "npResult2 {0}" , JsonConvert . SerializeObject ( npResult2 , Formatting . Indented ) ) ;
58
+ // var np2 = q.Select("np(it.NestedDto2.NestedDto3.Id)");
59
+ // var npResult2 = np2.ToDynamicList<int?>();
60
+ // Console.WriteLine("npResult2 {0}", JsonConvert.SerializeObject(npResult2, Formatting.Indented));
67
61
68
- var r1 = q . Select ( "it != null && it.NestedDto2 != null ? it.NestedDto2.Id : null" ) ;
69
- var list1 = r1 . ToDynamicList < int ? > ( ) ;
62
+ // var r1 = q.Select("it != null && it.NestedDto2 != null ? it.NestedDto2.Id : null");
63
+ // var list1 = r1.ToDynamicList<int?>();
70
64
71
- var r2 = q . Select ( "it != null && it.NestedDto2 != null ? it.NestedDto2 : null" ) ;
72
- var list2 = r2 . ToDynamicList < NestedDto2 > ( ) ;
65
+ // var r2 = q.Select("it != null && it.NestedDto2 != null ? it.NestedDto2 : null");
66
+ // var list2 = r2.ToDynamicList<NestedDto2>();
73
67
74
68
var config = new ParsingConfig
75
69
{
76
70
AllowNewToEvaluateAnyType = true ,
77
- CustomTypeProvider = new NetCore21CustomTypeProvider ( )
78
- } ;
79
-
80
- // Act
81
- var testDataAsQueryable = new List < string > { "name1" , "name2" } . AsQueryable ( ) ;
82
- var projectedData = ( IQueryable < NestedDto > ) testDataAsQueryable . Select ( config , $ "new { typeof ( NestedDto ) . FullName } (~ as Name)") ;
83
- Console . WriteLine ( projectedData . First ( ) . Name ) ;
84
- Console . WriteLine ( projectedData . Last ( ) . Name ) ;
85
-
86
- var all = new
87
- {
88
- test1 = new List < int > { 1 , 2 , 3 } . ToDynamicList ( typeof ( int ) ) ,
89
- test2 = new List < dynamic > { 4 , 5 , 6 } . ToDynamicList ( typeof ( int ) ) ,
90
- test3 = new List < object > { 7 , 8 , 9 } . ToDynamicList ( typeof ( int ) )
71
+ CustomTypeProvider = new TestCustomTypeProvider ( )
91
72
} ;
92
- Console . WriteLine ( "all {0}" , JsonConvert . SerializeObject ( all , Formatting . Indented ) ) ;
93
73
94
- var anyTest = new [ ]
95
- {
96
- new { id = "1" , values = new [ ] { 1 , 2 , 3 } } ,
97
- new { id = "2" , values = new [ ] { 1 , 4 } } ,
98
- new { id = "3" , values = new [ ] { 9 , 5 } }
99
- } . AsQueryable ( ) ;
74
+ //// Act
75
+ //var testDataAsQueryable = new List<string> { "name1", "name2" }.AsQueryable();
76
+ //var projectedData = (IQueryable<NestedDto>)testDataAsQueryable.Select(config, $"new {typeof(NestedDto).FullName}(~ as Name)");
77
+ //Console.WriteLine(projectedData.First().Name);
78
+ //Console.WriteLine(projectedData.Last().Name);
100
79
101
- var any1 = anyTest . Where ( x => x . values . Contains ( 1 ) ) ;
102
- Console . WriteLine ( "any1 {0}" , JsonConvert . SerializeObject ( any1 , Formatting . Indented ) ) ;
80
+ //var all = new
81
+ //{
82
+ // test1 = new List<int> { 1, 2, 3 }.ToDynamicList(typeof(int)),
83
+ // test2 = new List<dynamic> { 4, 5, 6 }.ToDynamicList(typeof(int)),
84
+ // test3 = new List<object> { 7, 8, 9 }.ToDynamicList(typeof(int))
85
+ //};
86
+ //Console.WriteLine("all {0}", JsonConvert.SerializeObject(all, Formatting.Indented));
103
87
104
- var any2 = anyTest . Where ( "values.Contains(1)" ) ;
105
- Console . WriteLine ( "any2 {0}" , JsonConvert . SerializeObject ( any2 , Formatting . Indented ) ) ;
88
+ //var anyTest = new[]
89
+ //{
90
+ // new { id = "1", values =new [] { 1, 2, 3 } },
91
+ // new { id = "2", values =new [] { 1, 4 } },
92
+ // new { id = "3", values =new [] { 9, 5 } }
93
+ //}.AsQueryable();
106
94
95
+ //var any1 = anyTest.Where(x => x.values.Contains(1));
96
+ //Console.WriteLine("any1 {0}", JsonConvert.SerializeObject(any1, Formatting.Indented));
107
97
98
+ //var any2 = anyTest.Where("values.Contains(1)");
99
+ //Console.WriteLine("any2 {0}", JsonConvert.SerializeObject(any2, Formatting.Indented));
108
100
109
- var dateLastModified = new DateTime ( 2018 , 1 , 15 ) ;
101
+ DateTime dateLastModified = new DateTime ( 2018 , 1 , 15 ) ;
110
102
111
103
var context = new TestContext ( ) ;
112
104
context . Cars . Add ( new Car { Brand = "Ford" , Color = "Blue" , Vin = "yes" , Year = "2017" , DateLastModified = dateLastModified } ) ;
@@ -120,19 +112,48 @@ static void Main(string[] args)
120
112
context . Brands . Add ( new Brand { BrandType = "Alfa" , BrandName = "Romeo" } ) ;
121
113
context . SaveChanges ( ) ;
122
114
123
- context . BaseDtos . Add ( new TestDto { BaseName = "b" , Name = "t" } ) ;
124
- context . BaseDtos . Add ( new OtherTestDto { BaseName = "b" , Name = "t" } ) ;
115
+ var testDto1 = new TestDto { BaseName = "a" , Name = "t" } ;
116
+ context . BaseDtos . Add ( testDto1 ) ;
117
+ var testDto2 = new TestDto { BaseName = "b" , Name = "t" } ;
118
+ context . BaseDtos . Add ( testDto2 ) ;
119
+
120
+ var otherTestDto = new OtherTestDto { BaseName = "c" , Name = "t" } ;
121
+ context . BaseDtos . Add ( otherTestDto ) ;
122
+ context . SaveChanges ( ) ;
123
+
124
+ context . ComplexDtos . Add ( new ComplexDto { X = "both" , ListOfBaseDtos = new BaseDto [ ] { testDto1 , otherTestDto } } ) ;
125
+ context . ComplexDtos . Add ( new ComplexDto { X = "testDto" , ListOfBaseDtos = new BaseDto [ ] { testDto2 } } ) ;
125
126
context . SaveChanges ( ) ;
126
127
127
- var oftypeTestDto1 = context . BaseDtos . OfType < TestDto > ( ) . Where ( x => x . Name == "t" ) . ToArray ( ) ;
128
- var oftypeTestDto2 = context . BaseDtos . OfType < TestDto > ( ) . Where ( "Name == \" t\" " ) . ToArray ( ) ;
128
+ OfTypeAndCastTests ( context , config ) ;
129
129
130
130
var carDateLastModified = context . Cars . Where ( config , "DateLastModified > \" 2018-01-16\" " ) ;
131
131
Console . WriteLine ( "carDateLastModified {0}" , JsonConvert . SerializeObject ( carDateLastModified , Formatting . Indented ) ) ;
132
132
133
- //var carFirstOrDefault = context.Cars.Where(config, "Brand == \"Ford\"");
134
- //Console.WriteLine("carFirstOrDefault {0}", JsonConvert.SerializeObject(carFirstOrDefault, Formatting.Indented));
133
+ var carFirstOrDefault = context . Cars . Where ( config , "Brand == \" Ford\" " ) ;
134
+ Console . WriteLine ( "carFirstOrDefault {0}" , JsonConvert . SerializeObject ( carFirstOrDefault , Formatting . Indented ) ) ;
135
+
136
+ LikeTests ( context , config ) ;
137
+
138
+ var testDynamic = context . Cars . Select ( c => new
139
+ {
140
+ K = c . Key ,
141
+ C = c . Color
142
+ } ) ;
143
+
144
+ var testDynamicResult = testDynamic . Select ( "it" ) . OrderBy ( "C" ) ;
145
+ try
146
+ {
147
+ Console . WriteLine ( "resultX {0}" , JsonConvert . SerializeObject ( testDynamicResult , Formatting . Indented ) ) ;
148
+ }
149
+ catch ( Exception e )
150
+ {
151
+ Console . WriteLine ( e ) ;
152
+ }
153
+ }
135
154
155
+ private static void LikeTests ( TestContext context , ParsingConfig config )
156
+ {
136
157
//var carsLike1 =
137
158
// from c in context.Cars
138
159
// where EF.Functions.Like(c.Brand, "%a%")
@@ -142,33 +163,66 @@ static void Main(string[] args)
142
163
//var cars2Like = context.Cars.Where(c => EF.Functions.Like(c.Brand, "%a%"));
143
164
//Console.WriteLine("cars2Like {0}", JsonConvert.SerializeObject(cars2Like, Formatting.Indented));
144
165
145
- // var dynamicCarsLike1 = context.Cars.Where(config, "TestContext.Like(Brand, \"%a%\")");
146
- // Console.WriteLine("dynamicCarsLike1 {0}", JsonConvert.SerializeObject(dynamicCarsLike1, Formatting.Indented));
166
+ var dynamicCarsLike1 = context . Cars . Where ( config , "TestContext.Like(Brand, \" %a%\" )" ) ;
167
+ Console . WriteLine ( "dynamicCarsLike1 {0}" , JsonConvert . SerializeObject ( dynamicCarsLike1 , Formatting . Indented ) ) ;
147
168
148
- // var dynamicCarsLike2 = context.Cars.Where(config, "TestContext.Like(Brand, \"%d%\")");
149
- // Console.WriteLine("dynamicCarsLike2 {0}", JsonConvert.SerializeObject(dynamicCarsLike2, Formatting.Indented));
169
+ var dynamicCarsLike2 = context . Cars . Where ( config , "TestContext.Like(Brand, \" %d%\" )" ) ;
170
+ Console . WriteLine ( "dynamicCarsLike2 {0}" , JsonConvert . SerializeObject ( dynamicCarsLike2 , Formatting . Indented ) ) ;
150
171
151
172
//var dynamicFunctionsLike1 = context.Cars.Where(config, "DynamicFunctions.Like(Brand, \"%a%\")");
152
- //Console.WriteLine("dynamicFunctionsLike1 {0}", JsonConvert.SerializeObject(dynamicFunctionsLike1, Formatting.Indented));
173
+ //Console.WriteLine("dynamicFunctionsLike1 {0}",
174
+ //JsonConvert.SerializeObject(dynamicFunctionsLike1, Formatting.Indented));
153
175
154
176
//var dynamicFunctionsLike2 = context.Cars.Where(config, "DynamicFunctions.Like(Vin, \"%a.%b%\", \".\")");
155
- //Console.WriteLine("dynamicFunctionsLike2 {0}", JsonConvert.SerializeObject(dynamicFunctionsLike2, Formatting.Indented));
177
+ //Console.WriteLine("dynamicFunctionsLike2 {0}",
178
+ //JsonConvert.SerializeObject(dynamicFunctionsLike2, Formatting.Indented));
179
+ }
156
180
157
- //var testDynamic = context.Cars.Select(c => new
158
- // {
159
- // K = c.Key,
160
- // C = c.Color
161
- //} );
181
+ private static void OfTypeAndCastTests ( TestContext context , ParsingConfig config )
182
+ {
183
+ var cast = context . BaseDtos . Where ( b => b is TestDto ) . Cast < TestDto > ( ) . ToArray ( ) ;
184
+ var castDynamicWithType = context . BaseDtos . Where ( b => b is TestDto ) . Cast ( typeof ( TestDto ) ) . ToDynamicArray ( ) ;
185
+ var castDynamicWithString = context . BaseDtos . Where ( b => b is TestDto ) . Cast ( config , "ConsoleAppEF2.Database.TestDto" ) . ToDynamicArray ( ) ;
162
186
163
- //var testDynamicResult = testDynamic.Select("it").OrderBy("C");
164
- //try
165
- //{
166
- // Console.WriteLine("resultX {0}", JsonConvert.SerializeObject(testDynamicResult, Formatting.Indented));
167
- //}
168
- //catch (Exception e)
169
- //{
170
- // Console.WriteLine(e);
171
- //}
187
+ var oftype = context . BaseDtos . OfType < TestDto > ( ) . ToArray ( ) ;
188
+ bool ofTypeAny = context . BaseDtos . OfType < TestDto > ( ) . Any ( ) ;
189
+ var oftypeDynamicWithType = context . BaseDtos . OfType ( typeof ( TestDto ) ) . ToDynamicArray ( ) ;
190
+ var oftypeDynamicWithString = context . BaseDtos . OfType ( config , "ConsoleAppEF2.Database.TestDto" ) . ToDynamicArray ( ) ;
191
+
192
+ var configX = new ParsingConfig
193
+ {
194
+ ResolveTypesBySimpleName = true
195
+ } ;
196
+ var oftypeDynamicWithSimpleNameString = context . BaseDtos . OfType ( configX , "TestDto" ) . ToDynamicArray ( ) ;
197
+
198
+ int isOfType = context . BaseDtos . Count ( b => b is TestDto ) ;
199
+ int isOfTypeDynamicTestDto = context . BaseDtos . Count ( config , "is(\" ConsoleAppEF2.Database.TestDto\" )" ) ;
200
+ int isOfTypeDynamicOtherTestDto = context . BaseDtos . Count ( config , "is(\" ConsoleAppEF2.Database.OtherTestDto\" )" ) ;
201
+ int isOfTypeDynamicComplexDto = context . BaseDtos . Count ( config , "is(\" ConsoleAppEF2.Database.ComplexDto\" )" ) ;
202
+
203
+ var asOfType = context . BaseDtos . Where ( b => b as TestDto != null ) . ToArray ( ) ;
204
+ var asOfTypeDynamicTestDto = context . BaseDtos . Where ( config , "As(\" ConsoleAppEF2.Database.TestDto\" ) != null" ) . ToDynamicArray ( ) ;
205
+ var asOfTypeDynamicOtherTestDto = context . BaseDtos . Where ( config , "As(\" ConsoleAppEF2.Database.OtherTestDto\" ) != null" ) . ToDynamicArray ( ) ;
206
+ var asOfTypeDynamicComplexDto = context . BaseDtos . Where ( config , "As(\" ConsoleAppEF2.Database.ComplexDto\" ) != null" ) . ToDynamicArray ( ) ;
207
+
208
+ var castOnX = context . BaseDtos . Where ( b => b as TestDto != null ) . Where ( b => ( ( TestDto ) b ) . Name != null ) . ToArray ( ) ;
209
+ var castOnXDynamic = context . BaseDtos . Where ( b => b as TestDto != null ) . Where ( config , "Cast(\" ConsoleAppEF2.Database.TestDto\" ).Name != null" ) . ToArray ( ) ;
210
+
211
+ var oftypeTestDto = context . BaseDtos . OfType < TestDto > ( ) . Where ( x => x . Name == "t" ) . ToArray ( ) ;
212
+ var oftypeTestDtoDynamic = context . BaseDtos . OfType < TestDto > ( ) . Where ( "Name == \" t\" " ) . ToArray ( ) ;
213
+
214
+ var complexOfType = context . ComplexDtos . Select ( c => c . ListOfBaseDtos . OfType < TestDto > ( ) . Where ( x => x . Name == "t" ) )
215
+ . ToArray ( ) ;
216
+ var complexOfTypeDynamic = context . ComplexDtos
217
+ . Select ( config , "ListOfBaseDtos.OfType(\" ConsoleAppEF2.Database.TestDto\" ).Where(Name == \" t\" )" )
218
+ . ToDynamicArray ( ) ;
219
+
220
+ var complexCast = context . ComplexDtos . Where ( c => c . X == "testDto" ) . ToList ( )
221
+ . Select ( c => c . ListOfBaseDtos . Cast < TestDto > ( ) . Where ( x => x . Name == "t" ) )
222
+ . ToArray ( ) ;
223
+ var complexCastDynamic = context . ComplexDtos . Where ( c => c . X == "testDto" ) . ToList ( ) . AsQueryable ( )
224
+ . Select ( config , "ListOfBaseDtos.Cast(\" ConsoleAppEF2.Database.TestDto\" ).Where(Name == \" t\" )" )
225
+ . ToDynamicArray ( ) ;
172
226
}
173
227
}
174
228
}
0 commit comments