Skip to content

Commit a48941b

Browse files
committed
Add examples for .NET 10
1 parent 4cc5d17 commit a48941b

File tree

5 files changed

+373
-1
lines changed

5 files changed

+373
-1
lines changed

System.Linq.Dynamic.Core.sln

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Linq.Dynamic.Core.Te
161161
EndProject
162162
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleAppPerformanceTest", "src-console\ConsoleAppPerformanceTest\ConsoleAppPerformanceTest.csproj", "{067C00CF-29FA-4643-814D-3A3C3C84634F}"
163163
EndProject
164+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp_net10", "src-console\ConsoleApp_net10\ConsoleApp_net10.csproj", "{34C58129-07DB-287E-A29B-FA8EE8BAEB05}"
165+
EndProject
164166
Global
165167
GlobalSection(SolutionConfigurationPlatforms) = preSolution
166168
Debug|Any CPU = Debug|Any CPU
@@ -1057,6 +1059,22 @@ Global
10571059
{067C00CF-29FA-4643-814D-3A3C3C84634F}.Release|x64.Build.0 = Release|Any CPU
10581060
{067C00CF-29FA-4643-814D-3A3C3C84634F}.Release|x86.ActiveCfg = Release|Any CPU
10591061
{067C00CF-29FA-4643-814D-3A3C3C84634F}.Release|x86.Build.0 = Release|Any CPU
1062+
{34C58129-07DB-287E-A29B-FA8EE8BAEB05}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1063+
{34C58129-07DB-287E-A29B-FA8EE8BAEB05}.Debug|Any CPU.Build.0 = Debug|Any CPU
1064+
{34C58129-07DB-287E-A29B-FA8EE8BAEB05}.Debug|ARM.ActiveCfg = Debug|Any CPU
1065+
{34C58129-07DB-287E-A29B-FA8EE8BAEB05}.Debug|ARM.Build.0 = Debug|Any CPU
1066+
{34C58129-07DB-287E-A29B-FA8EE8BAEB05}.Debug|x64.ActiveCfg = Debug|Any CPU
1067+
{34C58129-07DB-287E-A29B-FA8EE8BAEB05}.Debug|x64.Build.0 = Debug|Any CPU
1068+
{34C58129-07DB-287E-A29B-FA8EE8BAEB05}.Debug|x86.ActiveCfg = Debug|Any CPU
1069+
{34C58129-07DB-287E-A29B-FA8EE8BAEB05}.Debug|x86.Build.0 = Debug|Any CPU
1070+
{34C58129-07DB-287E-A29B-FA8EE8BAEB05}.Release|Any CPU.ActiveCfg = Release|Any CPU
1071+
{34C58129-07DB-287E-A29B-FA8EE8BAEB05}.Release|Any CPU.Build.0 = Release|Any CPU
1072+
{34C58129-07DB-287E-A29B-FA8EE8BAEB05}.Release|ARM.ActiveCfg = Release|Any CPU
1073+
{34C58129-07DB-287E-A29B-FA8EE8BAEB05}.Release|ARM.Build.0 = Release|Any CPU
1074+
{34C58129-07DB-287E-A29B-FA8EE8BAEB05}.Release|x64.ActiveCfg = Release|Any CPU
1075+
{34C58129-07DB-287E-A29B-FA8EE8BAEB05}.Release|x64.Build.0 = Release|Any CPU
1076+
{34C58129-07DB-287E-A29B-FA8EE8BAEB05}.Release|x86.ActiveCfg = Release|Any CPU
1077+
{34C58129-07DB-287E-A29B-FA8EE8BAEB05}.Release|x86.Build.0 = Release|Any CPU
10601078
EndGlobalSection
10611079
GlobalSection(SolutionProperties) = preSolution
10621080
HideSolutionNode = FALSE
@@ -1117,6 +1135,7 @@ Global
11171135
{C774DAE7-54A0-4FCD-A3B7-3CB63D7E112D} = {DBD7D9B6-FCC7-4650-91AF-E6457573A68F}
11181136
{CEBE3A33-4814-42A4-BD8E-F7F2308A4C8C} = {8463ED7E-69FB-49AE-85CF-0791AFD98E38}
11191137
{067C00CF-29FA-4643-814D-3A3C3C84634F} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
1138+
{34C58129-07DB-287E-A29B-FA8EE8BAEB05} = {7971CAEB-B9F2-416B-966D-2D697C4C1E62}
11201139
EndGlobalSection
11211140
GlobalSection(ExtensibilityGlobals) = postSolution
11221141
SolutionGuid = {94C56722-194E-4B8B-BC23-B3F754E89A20}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<RootNamespace>ConsoleApp</RootNamespace>
7+
<Nullable>enable</Nullable>
8+
<LangVersion>latest</LangVersion>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
13+
<!--<PackageReference Include="Z.DynamicLinq.NewtonsoftJson" Version="1.6.4" />
14+
<PackageReference Include="Z.DynamicLinq.SystemTextJson" Version="1.6.4" />-->
15+
</ItemGroup>
16+
17+
<ItemGroup>
18+
<ProjectReference Include="..\..\src\System.Linq.Dynamic.Core.NewtonsoftJson\System.Linq.Dynamic.Core.NewtonsoftJson.csproj" />
19+
<ProjectReference Include="..\..\src\System.Linq.Dynamic.Core.SystemTextJson\System.Linq.Dynamic.Core.SystemTextJson.csproj" />
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections;
3+
4+
namespace ConsoleApp_net6._0;
5+
6+
public class DataColumnOrdinalIgnoreCaseComparer : IComparer
7+
{
8+
public int Compare(object? x, object? y)
9+
{
10+
if (x == null && y == null)
11+
{
12+
return 0;
13+
}
14+
15+
if (x == null)
16+
{
17+
return -1;
18+
}
19+
20+
if (y == null)
21+
{
22+
return 1;
23+
}
24+
25+
if (x is string xAsString && y is string yAsString)
26+
{
27+
return StringComparer.OrdinalIgnoreCase.Compare(xAsString, yAsString);
28+
}
29+
30+
return Comparer.Default.Compare(x, y);
31+
}
32+
}
Lines changed: 299 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,299 @@
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

Comments
 (0)