-
-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathProgram.cs
More file actions
269 lines (217 loc) · 7.71 KB
/
Program.cs
File metadata and controls
269 lines (217 loc) · 7.71 KB
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
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
62
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
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Linq.Dynamic.Core.NewtonsoftJson;
using System.Linq.Dynamic.Core.SystemTextJson;
using System.Linq.Expressions;
using System.Text.Json;
using Newtonsoft.Json.Linq;
namespace ConsoleApp_net6._0;
public class X
{
public string Key { get; set; } = null!;
public List<Y>? Contestants { get; set; }
}
public class Y
{
}
public class SalesData
{
public string Region { get; set; }
public string Product { get; set; }
public string Sales { get; set; }
}
public class GroupedSalesData
{
public string Region { get; set; }
public string? Product { get; set; }
public int TotalSales { get; set; }
public int GroupLevel { get; set; }
}
class Program
{
static void Main(string[] args)
{
Q912a();
Q912b();
return;
Json();
NewtonsoftJson();
return;
Issue389DoesNotWork();
return;
Issue389_Works();
return;
var q = new[]
{
new X { Key = "x" },
new X { Key = "a" },
new X { Key = "a", Contestants = new List<Y> { new() } }
}.AsQueryable();
var groupByKey = q.GroupBy("Key");
var selectQry = groupByKey.Select("new (Key, Sum(np(Contestants.Count, 0)) As TotalCount)").ToDynamicList();
Normal();
Dynamic();
}
private static void Q912a()
{
var extractedRows = new List<SalesData>
{
new() { Region = "North", Product = "Widget", Sales = "100" },
new() { Region = "North", Product = "Gadget", Sales = "150" },
new() { Region = "South", Product = "Widget", Sales = "200" },
new() { Region = "South", Product = "Gadget", Sales = "100" },
new() { Region = "North", Product = "Widget", Sales = "50" }
};
var rows = extractedRows.AsQueryable();
// GROUPING SET 1: (Region, Product)
var detailed = rows
.GroupBy("new (Region, Product)")
.Select<GroupedSalesData>("new (Key.Region as Region, Key.Product as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 0 as GroupLevel)");
// GROUPING SET 2: (Region)
var regionSubtotal = rows
.GroupBy("Region")
.Select<GroupedSalesData>("new (Key as Region, null as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 1 as GroupLevel)");
var combined = detailed.Concat(regionSubtotal).AsQueryable();
var ordered = combined.OrderBy("Product").ToDynamicList();
int x = 9;
}
private static void Q912b()
{
var eInfoJoinTable = new DataTable();
eInfoJoinTable.Columns.Add("Region", typeof(string));
eInfoJoinTable.Columns.Add("Product", typeof(string));
eInfoJoinTable.Columns.Add("Sales", typeof(int));
eInfoJoinTable.Rows.Add("North", "Apples", 100);
eInfoJoinTable.Rows.Add("North", "Oranges", 150);
eInfoJoinTable.Rows.Add("South", "Apples", 200);
eInfoJoinTable.Rows.Add("South", "Oranges", 250);
var extractedRows =
from row in eInfoJoinTable.AsEnumerable()
select row;
var rows = extractedRows.AsQueryable();
// GROUPING SET 1: (Region, Product)
var detailed = rows
.GroupBy("new (Region, Product)")
.Select("new (Key.Region as Region, Key.Product as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 0 as GroupLevel)");
// GROUPING SET 2: (Region)
var regionSubtotal = rows
.GroupBy("Region")
.Select("new (Key as Region, null as Product, Sum(Convert.ToInt32(Sales)) as TotalSales, 1 as GroupLevel)");
var combined = detailed.ToDynamicArray().Concat(regionSubtotal.ToDynamicArray()).AsQueryable();
var ordered = combined.OrderBy("Product").ToDynamicList();
int x = 9;
}
private static void NewtonsoftJson()
{
var array = JArray.Parse(@"[
{
""first"": 1,
""City"": ""Paris"",
""third"": ""test""
},
{
""first"": 2,
""City"": ""New York"",
""third"": ""abc""
}]");
var where = array.Where("City == @0", "Paris");
foreach (var result in where)
{
Console.WriteLine(result["first"]);
}
var select = array.Select("City");
foreach (var result in select)
{
Console.WriteLine(result);
}
var whereWithSelect = array.Where("City == @0", "Paris").Select("first");
foreach (var result in whereWithSelect)
{
Console.WriteLine(result);
}
}
private static void Json()
{
var doc = JsonDocument.Parse(@"[
{
""first"": 1,
""City"": ""Paris"",
""third"": ""test""
},
{
""first"": 2,
""City"": ""New York"",
""third"": ""abc""
}]");
var where = doc.Where("City == @0", "Paris");
foreach (var result in where.RootElement.EnumerateArray())
{
Console.WriteLine(result.GetProperty("first"));
}
var select = doc.Select("City");
foreach (var result in select.RootElement.EnumerateArray())
{
Console.WriteLine(result);
}
var whereWithSelect = doc.Where("City == @0", "Paris").Select("first");
foreach (var result in whereWithSelect.RootElement.EnumerateArray())
{
Console.WriteLine(result);
}
}
private static void Issue389_Works()
{
var strArray = new[] { "1", "2", "3", "4" };
var x = new List<ParameterExpression>();
x.Add(Expression.Parameter(strArray.GetType(), "strArray"));
string query = "string.Join(\",\", strArray)";
var e = DynamicExpressionParser.ParseLambda(x.ToArray(), null, query);
Delegate del = e.Compile();
var result1 = del.DynamicInvoke(new object?[] { strArray });
Console.WriteLine(result1);
}
private static void Issue389WorksWithInts()
{
var intArray = new object[] { 1, 2, 3, 4 };
var x = new List<ParameterExpression>();
x.Add(Expression.Parameter(intArray.GetType(), "intArray"));
string query = "string.Join(\",\", intArray)";
var e = DynamicExpressionParser.ParseLambda(x.ToArray(), null, query);
Delegate del = e.Compile();
var result = del.DynamicInvoke(new object?[] { intArray });
Console.WriteLine(result);
}
private static void Issue389DoesNotWork()
{
var intArray = new[] { 1, 2, 3, 4 };
var x = new List<ParameterExpression>();
x.Add(Expression.Parameter(intArray.GetType(), "intArray"));
string query = "string.Join(\",\", intArray)";
var e = DynamicExpressionParser.ParseLambda(x.ToArray(), null, query);
Delegate del = e.Compile();
var result = del.DynamicInvoke(new object?[] { intArray });
Console.WriteLine(result);
}
private static void Normal()
{
var e = new int[0].AsQueryable();
var q = new[] { 1 }.AsQueryable();
var a = q.FirstOrDefault();
var b = e.FirstOrDefault(44);
var c = q.FirstOrDefault(i => i == 0);
var d = q.FirstOrDefault(i => i == 0, 42);
var t = q.Take(1);
}
private static void Dynamic()
{
var e = new int[0].AsQueryable() as IQueryable;
var q = new[] { 1 }.AsQueryable() as IQueryable;
var a = q.FirstOrDefault();
//var b = e.FirstOrDefault(44);
var c = q.FirstOrDefault("it == 0");
//var d = q.FirstOrDefault(i => i == 0, 42);
}
}