1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Data ;
4+ using System . Linq ;
5+ using System . Linq . Dynamic . Core ;
6+ using System . Linq . Dynamic . Core . NewtonsoftJson ;
7+ using System . Linq . Dynamic . Core . SystemTextJson ;
8+ using System . Linq . Expressions ;
9+ using System . Text . Json ;
10+ using ConsoleApp_net6 . _0 ;
11+ using Newtonsoft . Json . Linq ;
12+
13+ namespace ConsoleApp ;
14+
15+ public class X
16+ {
17+ public string Key { get ; set ; } = null ! ;
18+
19+ public List < Y > ? Contestants { get ; set ; }
20+ }
21+
22+ public class Y
23+ {
24+ }
25+
26+ public class SalesData
27+ {
28+ public string Region { get ; set ; }
29+ public string Product { get ; set ; }
30+ public string Sales { get ; set ; }
31+ }
32+
33+ public class GroupedSalesData
34+ {
35+ public string Region { get ; set ; }
36+ public string ? Product { get ; set ; }
37+ public int TotalSales { get ; set ; }
38+ public int GroupLevel { get ; set ; }
39+ }
40+
41+ class Program
42+ {
43+ static void Main ( string [ ] args )
44+ {
45+ Issue918 ( ) ;
46+ return ;
47+
48+ Issue912a ( ) ;
49+ Issue912b ( ) ;
50+ return ;
51+
52+ Json ( ) ;
53+ NewtonsoftJson ( ) ;
54+
55+ return ;
56+
57+ Issue389DoesNotWork ( ) ;
58+ return ;
59+ Issue389_Works ( ) ;
60+ return ;
61+
62+ var q = new [ ]
63+ {
64+ new X { Key = "x" } ,
65+ new X { Key = "a" } ,
66+ new X { Key = "a" , Contestants = new List < Y > { new ( ) } }
67+ } . AsQueryable ( ) ;
68+ var groupByKey = q . GroupBy ( "Key" ) ;
69+ var selectQry = groupByKey . Select ( "new (Key, Sum(np(Contestants.Count, 0)) As TotalCount)" ) . ToDynamicList ( ) ;
70+
71+ Normal ( ) ;
72+ Dynamic ( ) ;
73+ }
74+
75+ private static void Issue918 ( )
76+ {
77+ var persons = new DataTable ( ) ;
78+ persons . Columns . Add ( "FirstName" , typeof ( string ) ) ;
79+ persons . Columns . Add ( "Nickname" , typeof ( string ) ) ;
80+ persons . Columns . Add ( "Income" , typeof ( decimal ) ) . AllowDBNull = true ;
81+
82+ // Adding sample data to the first DataTable
83+ persons . Rows . Add ( "alex" , DBNull . Value , 5000.50m ) ;
84+ persons . Rows . Add ( "MAGNUS" , "Mag" , 5000.50m ) ;
85+ persons . Rows . Add ( "Terry" , "Ter" , 4000.20m ) ;
86+ persons . Rows . Add ( "Charlotte" , "Charl" , DBNull . Value ) ;
87+
88+ var linqQuery =
89+ from personsRow in persons . AsEnumerable ( )
90+ select personsRow ;
91+
92+ var queryableRows = linqQuery . AsQueryable ( ) ;
93+
94+ // Sorted at the top of the list
95+ var comparer = new DataColumnOrdinalIgnoreCaseComparer ( ) ;
96+ var sortedRows = queryableRows . OrderBy ( "FirstName" , comparer ) . ToList ( ) ;
97+
98+ int xxx = 0 ;
99+ }
100+
101+ private static void Issue912a ( )
102+ {
103+ var extractedRows = new List < SalesData >
104+ {
105+ new ( ) { Region = "North" , Product = "Widget" , Sales = "100" } ,
106+ new ( ) { Region = "North" , Product = "Gadget" , Sales = "150" } ,
107+ new ( ) { Region = "South" , Product = "Widget" , Sales = "200" } ,
108+ new ( ) { Region = "South" , Product = "Gadget" , Sales = "100" } ,
109+ new ( ) { Region = "North" , Product = "Widget" , Sales = "50" }
110+ } ;
111+
112+ var rows = extractedRows . AsQueryable ( ) ;
113+
114+ // GROUPING SET 1: (Region, Product)
115+ var detailed = rows
116+ . GroupBy ( "new (Region, Product)" )
117+ . Select < GroupedSalesData > ( "new (Key.Region as Region, Key.Product as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 0 as GroupLevel)" ) ;
118+
119+ // GROUPING SET 2: (Region)
120+ var regionSubtotal = rows
121+ . GroupBy ( "Region" )
122+ . Select < GroupedSalesData > ( "new (Key as Region, null as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 1 as GroupLevel)" ) ;
123+
124+ var combined = detailed . Concat ( regionSubtotal ) . AsQueryable ( ) ;
125+ var ordered = combined . OrderBy ( "Product" ) . ToDynamicList ( ) ;
126+
127+ int x = 9 ;
128+ }
129+
130+ private static void Issue912b ( )
131+ {
132+ var eInfoJoinTable = new DataTable ( ) ;
133+ eInfoJoinTable . Columns . Add ( "Region" , typeof ( string ) ) ;
134+ eInfoJoinTable . Columns . Add ( "Product" , typeof ( string ) ) ;
135+ eInfoJoinTable . Columns . Add ( "Sales" , typeof ( int ) ) ;
136+
137+ eInfoJoinTable . Rows . Add ( "North" , "Apples" , 100 ) ;
138+ eInfoJoinTable . Rows . Add ( "North" , "Oranges" , 150 ) ;
139+ eInfoJoinTable . Rows . Add ( "South" , "Apples" , 200 ) ;
140+ eInfoJoinTable . Rows . Add ( "South" , "Oranges" , 250 ) ;
141+
142+ var extractedRows =
143+ from row in eInfoJoinTable . AsEnumerable ( )
144+ select row ;
145+
146+ var rows = extractedRows . AsQueryable ( ) ;
147+
148+ // GROUPING SET 1: (Region, Product)
149+ var detailed = rows
150+ . GroupBy ( "new (Region, Product)" )
151+ . Select ( "new (Key.Region as Region, Key.Product as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 0 as GroupLevel)" ) ;
152+
153+ // GROUPING SET 2: (Region)
154+ var regionSubtotal = rows
155+ . GroupBy ( "Region" )
156+ . Select ( "new (Key as Region, null as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 1 as GroupLevel)" ) ;
157+
158+ var combined = detailed . ToDynamicArray ( ) . Concat ( regionSubtotal . ToDynamicArray ( ) ) . AsQueryable ( ) ;
159+ var ordered = combined . OrderBy ( "Product" ) . ToDynamicList ( ) ;
160+
161+ int x = 9 ;
162+ }
163+
164+ private static void NewtonsoftJson ( )
165+ {
166+ var array = JArray . Parse ( @"[
167+ {
168+ ""first"": 1,
169+ ""City"": ""Paris"",
170+ ""third"": ""test""
171+ },
172+ {
173+ ""first"": 2,
174+ ""City"": ""New York"",
175+ ""third"": ""abc""
176+ }]" ) ;
177+
178+ var where = array . Where ( "City == @0" , "Paris" ) ;
179+ foreach ( var result in where )
180+ {
181+ Console . WriteLine ( result [ "first" ] ) ;
182+ }
183+
184+ var select = array . Select ( "City" ) ;
185+ foreach ( var result in select )
186+ {
187+ Console . WriteLine ( result ) ;
188+ }
189+
190+ var whereWithSelect = array . Where ( "City == @0" , "Paris" ) . Select ( "first" ) ;
191+ foreach ( var result in whereWithSelect )
192+ {
193+ Console . WriteLine ( result ) ;
194+ }
195+ }
196+
197+ private static void Json ( )
198+ {
199+ var doc = JsonDocument . Parse ( @"[
200+ {
201+ ""first"": 1,
202+ ""City"": ""Paris"",
203+ ""third"": ""test""
204+ },
205+ {
206+ ""first"": 2,
207+ ""City"": ""New York"",
208+ ""third"": ""abc""
209+ }]" ) ;
210+
211+ var where = doc . Where ( "City == @0" , "Paris" ) ;
212+ foreach ( var result in where . RootElement . EnumerateArray ( ) )
213+ {
214+ Console . WriteLine ( result . GetProperty ( "first" ) ) ;
215+ }
216+
217+ var select = doc . Select ( "City" ) ;
218+ foreach ( var result in select . RootElement . EnumerateArray ( ) )
219+ {
220+ Console . WriteLine ( result ) ;
221+ }
222+
223+ var whereWithSelect = doc . Where ( "City == @0" , "Paris" ) . Select ( "first" ) ;
224+ foreach ( var result in whereWithSelect . RootElement . EnumerateArray ( ) )
225+ {
226+ Console . WriteLine ( result ) ;
227+ }
228+ }
229+
230+ private static void Issue389_Works ( )
231+ {
232+ var strArray = new [ ] { "1" , "2" , "3" , "4" } ;
233+ var x = new List < ParameterExpression > ( ) ;
234+ x . Add ( Expression . Parameter ( strArray . GetType ( ) , "strArray" ) ) ;
235+
236+ string query = "string.Join(\" ,\" , strArray)" ;
237+
238+ var e = DynamicExpressionParser . ParseLambda ( x . ToArray ( ) , null , query ) ;
239+ Delegate del = e . Compile ( ) ;
240+ var result1 = del . DynamicInvoke ( new object ? [ ] { strArray } ) ;
241+ Console . WriteLine ( result1 ) ;
242+ }
243+
244+ private static void Issue389WorksWithInts ( )
245+ {
246+ var intArray = new object [ ] { 1 , 2 , 3 , 4 } ;
247+ var x = new List < ParameterExpression > ( ) ;
248+ x . Add ( Expression . Parameter ( intArray . GetType ( ) , "intArray" ) ) ;
249+
250+ string query = "string.Join(\" ,\" , intArray)" ;
251+
252+ var e = DynamicExpressionParser . ParseLambda ( x . ToArray ( ) , null , query ) ;
253+ Delegate del = e . Compile ( ) ;
254+ var result = del . DynamicInvoke ( new object ? [ ] { intArray } ) ;
255+
256+ Console . WriteLine ( result ) ;
257+ }
258+
259+ private static void Issue389DoesNotWork ( )
260+ {
261+ var intArray = new [ ] { 1 , 2 , 3 , 4 } ;
262+ var x = new List < ParameterExpression > ( ) ;
263+ x . Add ( Expression . Parameter ( intArray . GetType ( ) , "intArray" ) ) ;
264+
265+ string query = "string.Join(\" ,\" , intArray)" ;
266+
267+ var e = DynamicExpressionParser . ParseLambda ( x . ToArray ( ) , null , query ) ;
268+ Delegate del = e . Compile ( ) ;
269+ var result = del . DynamicInvoke ( new object ? [ ] { intArray } ) ;
270+
271+ Console . WriteLine ( result ) ;
272+ }
273+
274+ private static void Normal ( )
275+ {
276+ var e = new int [ 0 ] . AsQueryable ( ) ;
277+ var q = new [ ] { 1 } . AsQueryable ( ) ;
278+
279+ var a = q . FirstOrDefault ( ) ;
280+ var b = e . FirstOrDefault ( 44 ) ;
281+
282+ var c = q . FirstOrDefault ( i => i == 0 ) ;
283+ var d = q . FirstOrDefault ( i => i == 0 , 42 ) ;
284+
285+ var t = q . Take ( 1 ) ;
286+ }
287+
288+ private static void Dynamic ( )
289+ {
290+ var e = new int [ 0 ] . AsQueryable ( ) as IQueryable ;
291+ var q = new [ ] { 1 } . AsQueryable ( ) as IQueryable ;
292+
293+ var a = q . FirstOrDefault ( ) ;
294+ //var b = e.FirstOrDefault(44);
295+
296+ var c = q . FirstOrDefault ( "it == 0" ) ;
297+ //var d = q.FirstOrDefault(i => i == 0, 42);
298+ }
299+ }
0 commit comments