22using System . Collections . Generic ;
33using System . Linq ;
44using System . Linq . Dynamic . Core ;
5- using System . Linq . Dynamic . Core . Parser ;
5+ using System . Linq . Dynamic . Core . NewtonsoftJson ;
6+ using System . Linq . Dynamic . Core . SystemTextJson ;
67using System . Linq . Expressions ;
8+ using System . Text . Json ;
9+ using Newtonsoft . Json . Linq ;
710
8- namespace ConsoleApp_net6 . _0
11+ namespace ConsoleApp_net6 . _0 ;
12+
13+ public class X
914{
10- public class X
11- {
12- public string Key { get ; set ; } = null ! ;
15+ public string Key { get ; set ; } = null ! ;
1316
14- public List < Y > ? Contestants { get ; set ; }
15- }
17+ public List < Y > ? Contestants { get ; set ; }
18+ }
1619
17- public class Y
20+ public class Y
21+ {
22+ }
23+
24+ class Program
25+ {
26+ static void Main ( string [ ] args )
1827 {
28+ Json ( ) ;
29+ NewtonsoftJson ( ) ;
30+
31+ return ;
32+
33+ Issue389DoesNotWork ( ) ;
34+ return ;
35+ Issue389_Works ( ) ;
36+ return ;
37+
38+ var q = new [ ]
39+ {
40+ new X { Key = "x" } ,
41+ new X { Key = "a" } ,
42+ new X { Key = "a" , Contestants = new List < Y > { new Y ( ) } }
43+ } . AsQueryable ( ) ;
44+ var groupByKey = q . GroupBy ( "Key" ) ;
45+ var selectQry = groupByKey . Select ( "new (Key, Sum(np(Contestants.Count, 0)) As TotalCount)" ) . ToDynamicList ( ) ;
46+
47+ Normal ( ) ;
48+ Dynamic ( ) ;
1949 }
2050
21- class Program
51+ private static void NewtonsoftJson ( )
2252 {
23- static void Main ( string [ ] args )
53+ var array = JArray . Parse ( @"[
2454 {
25- var parser = new ExpressionParser ( new [ ] { Expression . Parameter ( typeof ( int ) , "VarA" ) } , "\" foo\" & VarA" , new object [ 0 ] , new ParsingConfig { ConvertObjectToSupportComparison = true } ) ;
26-
27- var expression = parser . Parse ( typeof ( string ) ) ;
28-
29- Issue389DoesNotWork ( ) ;
30- return ;
31- Issue389_Works ( ) ;
32- return ;
33-
34- var q = new [ ]
35- {
36- new X { Key = "x" } ,
37- new X { Key = "a" } ,
38- new X { Key = "a" , Contestants = new List < Y > { new Y ( ) } }
39- } . AsQueryable ( ) ;
40- var groupByKey = q . GroupBy ( "Key" ) ;
41- var selectQry = groupByKey . Select ( "new (Key, Sum(np(Contestants.Count, 0)) As TotalCount)" ) . ToDynamicList ( ) ;
42-
43- Normal ( ) ;
44- Dynamic ( ) ;
45- }
46-
47- private static void Issue389_Works ( )
55+ ""first"": 1,
56+ ""City"": ""Paris"",
57+ ""third"": ""test""
58+ },
4859 {
49- var strArray = new [ ] { "1" , "2" , "3" , "4" } ;
50- var x = new List < ParameterExpression > ( ) ;
51- x . Add ( Expression . Parameter ( strArray . GetType ( ) , "strArray" ) ) ;
60+ ""first"": 2,
61+ ""City"": ""New York"",
62+ ""third"": ""abc""
63+ }]" ) ;
5264
53- string query = "string.Join(\" ,\" , strArray)" ;
65+ var where = array . Where ( "City == @0" , "Paris" ) ;
66+ foreach ( var result in where )
67+ {
68+ Console . WriteLine ( result [ "first" ] ) ;
69+ }
5470
55- var e = DynamicExpressionParser . ParseLambda ( x . ToArray ( ) , null , query ) ;
56- Delegate del = e . Compile ( ) ;
57- var result1 = del . DynamicInvoke ( new object ? [ ] { strArray } ) ;
58- Console . WriteLine ( result1 ) ;
71+ var select = array . Select ( "City" ) ;
72+ foreach ( var result in select )
73+ {
74+ Console . WriteLine ( result ) ;
5975 }
6076
61- private static void Issue389WorksWithInts ( )
77+ var whereWithSelect = array . Where ( "City == @0" , "Paris" ) . Select ( "first" ) ;
78+ foreach ( var result in whereWithSelect )
6279 {
63- var intArray = new object [ ] { 1 , 2 , 3 , 4 } ;
64- var x = new List < ParameterExpression > ( ) ;
65- x . Add ( Expression . Parameter ( intArray . GetType ( ) , "intArray" ) ) ;
80+ Console . WriteLine ( result ) ;
81+ }
82+ }
6683
67- string query = "string.Join(\" ,\" , intArray)" ;
84+ private static void Json ( )
85+ {
86+ var doc = JsonDocument . Parse ( @"[
87+ {
88+ ""first"": 1,
89+ ""City"": ""Paris"",
90+ ""third"": ""test""
91+ },
92+ {
93+ ""first"": 2,
94+ ""City"": ""New York"",
95+ ""third"": ""abc""
96+ }]" ) ;
6897
69- var e = DynamicExpressionParser . ParseLambda ( x . ToArray ( ) , null , query ) ;
70- Delegate del = e . Compile ( ) ;
71- var result = del . DynamicInvoke ( new object ? [ ] { intArray } ) ;
98+ var where = doc . Where ( "City == @0" , "Paris" ) ;
99+ foreach ( var result in where . RootElement . EnumerateArray ( ) )
100+ {
101+ Console . WriteLine ( result . GetProperty ( "first" ) ) ;
102+ }
72103
104+ var select = doc . Select ( "City" ) ;
105+ foreach ( var result in select . RootElement . EnumerateArray ( ) )
106+ {
73107 Console . WriteLine ( result ) ;
74108 }
75109
76- private static void Issue389DoesNotWork ( )
110+ var whereWithSelect = doc . Where ( "City == @0" , "Paris" ) . Select ( "first" ) ;
111+ foreach ( var result in whereWithSelect . RootElement . EnumerateArray ( ) )
77112 {
78- var intArray = new [ ] { 1 , 2 , 3 , 4 } ;
79- var x = new List < ParameterExpression > ( ) ;
80- x . Add ( Expression . Parameter ( intArray . GetType ( ) , "intArray" ) ) ;
113+ Console . WriteLine ( result ) ;
114+ }
115+ }
81116
82- string query = "string.Join(\" ,\" , intArray)" ;
117+ private static void Issue389_Works ( )
118+ {
119+ var strArray = new [ ] { "1" , "2" , "3" , "4" } ;
120+ var x = new List < ParameterExpression > ( ) ;
121+ x . Add ( Expression . Parameter ( strArray . GetType ( ) , "strArray" ) ) ;
83122
84- var e = DynamicExpressionParser . ParseLambda ( x . ToArray ( ) , null , query ) ;
85- Delegate del = e . Compile ( ) ;
86- var result = del . DynamicInvoke ( new object ? [ ] { intArray } ) ;
123+ string query = "string.Join(\" ,\" , strArray)" ;
87124
88- Console . WriteLine ( result ) ;
89- }
125+ var e = DynamicExpressionParser . ParseLambda ( x . ToArray ( ) , null , query ) ;
126+ Delegate del = e . Compile ( ) ;
127+ var result1 = del . DynamicInvoke ( new object ? [ ] { strArray } ) ;
128+ Console . WriteLine ( result1 ) ;
129+ }
90130
91- private static void Normal ( )
92- {
93- var e = new int [ 0 ] . AsQueryable ( ) ;
94- var q = new [ ] { 1 } . AsQueryable ( ) ;
131+ private static void Issue389WorksWithInts ( )
132+ {
133+ var intArray = new object [ ] { 1 , 2 , 3 , 4 } ;
134+ var x = new List < ParameterExpression > ( ) ;
135+ x . Add ( Expression . Parameter ( intArray . GetType ( ) , "intArray" ) ) ;
95136
96- var a = q . FirstOrDefault ( ) ;
97- var b = e . FirstOrDefault ( 44 ) ;
137+ string query = "string.Join(\" ,\" , intArray)" ;
98138
99- var c = q . FirstOrDefault ( i => i == 0 ) ;
100- var d = q . FirstOrDefault ( i => i == 0 , 42 ) ;
139+ var e = DynamicExpressionParser . ParseLambda ( x . ToArray ( ) , null , query ) ;
140+ Delegate del = e . Compile ( ) ;
141+ var result = del . DynamicInvoke ( new object ? [ ] { intArray } ) ;
101142
102- var t = q . Take ( 1 ) ;
103- }
143+ Console . WriteLine ( result ) ;
144+ }
104145
105- private static void Dynamic ( )
106- {
107- var e = new int [ 0 ] . AsQueryable ( ) as IQueryable ;
108- var q = new [ ] { 1 } . AsQueryable ( ) as IQueryable ;
146+ private static void Issue389DoesNotWork ( )
147+ {
148+ var intArray = new [ ] { 1 , 2 , 3 , 4 } ;
149+ var x = new List < ParameterExpression > ( ) ;
150+ x . Add ( Expression . Parameter ( intArray . GetType ( ) , "intArray" ) ) ;
109151
110- var a = q . FirstOrDefault ( ) ;
111- //var b = e.FirstOrDefault(44);
152+ string query = "string.Join(\" ,\" , intArray)" ;
112153
113- var c = q . FirstOrDefault ( "it == 0" ) ;
114- //var d = q.FirstOrDefault(i => i == 0, 42);
115- }
154+ var e = DynamicExpressionParser . ParseLambda ( x . ToArray ( ) , null , query ) ;
155+ Delegate del = e . Compile ( ) ;
156+ var result = del . DynamicInvoke ( new object ? [ ] { intArray } ) ;
157+
158+ Console . WriteLine ( result ) ;
159+ }
160+
161+ private static void Normal ( )
162+ {
163+ var e = new int [ 0 ] . AsQueryable ( ) ;
164+ var q = new [ ] { 1 } . AsQueryable ( ) ;
165+
166+ var a = q . FirstOrDefault ( ) ;
167+ var b = e . FirstOrDefault ( 44 ) ;
168+
169+ var c = q . FirstOrDefault ( i => i == 0 ) ;
170+ var d = q . FirstOrDefault ( i => i == 0 , 42 ) ;
171+
172+ var t = q . Take ( 1 ) ;
173+ }
174+
175+ private static void Dynamic ( )
176+ {
177+ var e = new int [ 0 ] . AsQueryable ( ) as IQueryable ;
178+ var q = new [ ] { 1 } . AsQueryable ( ) as IQueryable ;
179+
180+ var a = q . FirstOrDefault ( ) ;
181+ //var b = e.FirstOrDefault(44);
182+
183+ var c = q . FirstOrDefault ( "it == 0" ) ;
184+ //var d = q.FirstOrDefault(i => i == 0, 42);
116185 }
117186}
0 commit comments