Skip to content

Commit ca95bdd

Browse files
committed
Add project files.
1 parent 20a6756 commit ca95bdd

File tree

9 files changed

+478
-0
lines changed

9 files changed

+478
-0
lines changed

DynamicLinq/DynamicLinq.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

DynamicLinq/DynamicLinqSample.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace DynamicLinq
2+
{
3+
public class DynamicLinqSample
4+
{
5+
6+
}
7+
}

DynamicLinqSolution.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.9.34414.90
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hogwards.Models", "Hogwards.Models\Hogwards.Models.csproj", "{43BA7485-C76A-4BA1-B168-0B3A546556AE}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DynamicLinqTestConsoleApp", "DynamicLinqTestConsoleApp\DynamicLinqTestConsoleApp.csproj", "{12697E47-C892-4370-BCD9-544498E6D44D}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{43BA7485-C76A-4BA1-B168-0B3A546556AE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{43BA7485-C76A-4BA1-B168-0B3A546556AE}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{43BA7485-C76A-4BA1-B168-0B3A546556AE}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{43BA7485-C76A-4BA1-B168-0B3A546556AE}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{12697E47-C892-4370-BCD9-544498E6D44D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{12697E47-C892-4370-BCD9-544498E6D44D}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{12697E47-C892-4370-BCD9-544498E6D44D}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{12697E47-C892-4370-BCD9-544498E6D44D}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {6FD4875D-892E-48D4-A451-05C1825A0CCE}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
12+
<PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.7" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\Hogwards.Models\Hogwards.Models.csproj" />
17+
</ItemGroup>
18+
19+
</Project>

DynamicLinqTestConsoleApp/Program.cs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using Hogwards.Models;
2+
using System.Linq.Dynamic.Core;
3+
using System.Linq.Expressions;
4+
using Microsoft.EntityFrameworkCore;
5+
6+
var studentData = new StudentData();
7+
var students = studentData.GetStudents();
8+
9+
//House.Name != null && House.Name == "RavenClaw"
10+
Console.WriteLine("----Where condition on property under a sub object----");
11+
Console.WriteLine("----House.Name == RavenClaw----");
12+
Console.WriteLine("----Not null check using np() function----");
13+
14+
var studentByHouseResult = students
15+
.Where("np(House.Name) == \"Ravenclaw\"")
16+
.ToList();
17+
18+
foreach (var student in studentByHouseResult)
19+
{
20+
Console.WriteLine("{0}\t\t{1}\t{2}", student.Name, student.House?.Name, student.Age);
21+
}
22+
23+
24+
25+
//Dynamically creating lambda expressions
26+
Console.WriteLine("\n----Dynamic Lambda----");
27+
Console.WriteLine("----Students older than 14----");
28+
29+
Expression<Func<Student, bool>> itExpression = DynamicExpressionParser
30+
.ParseLambda<Student, bool>(new ParsingConfig(), true, "House.Name == @0", "Hufflepuff");
31+
32+
Expression<Func<Student, bool>> ageExpression = DynamicExpressionParser
33+
.ParseLambda<Student, bool>(new ParsingConfig(), true, "Age >= @0", 14);
34+
35+
var conditionCheck = "age";
36+
var lambdaResult = students
37+
.Where("@0(it)", conditionCheck == "house" ? itExpression : ageExpression)
38+
.ToList();
39+
40+
foreach (var student in lambdaResult)
41+
{
42+
Console.WriteLine("{0}\t\t{1}\t{2}", student.Name, student.House?.Name, student.Age);
43+
}
44+
45+
46+
47+
//Group sort
48+
Console.WriteLine("\n----Group Sort----");
49+
Console.WriteLine("----Sort by house by descending order, then sort the students starting from elder to younger under each house----");
50+
51+
var groupSortResult = students
52+
.OrderBy("np(House.Name, \"\") asc, age desc")
53+
.ToList();
54+
55+
foreach (var student in groupSortResult)
56+
{
57+
Console.WriteLine("{0}\t\t{1}\t{2}", student.Name, student.House?.Name, student.Age);
58+
}
59+
60+
61+
62+
//Selecting Data Using Dynamic LINQ
63+
//public List<Student> GetStudents() =>
64+
// students
65+
// .Select(s => new Student
66+
// {
67+
// Name = s.Name,
68+
// age = s.Age,
69+
// House = s.House.House,
70+
// })
71+
// .ToList();
72+
73+
Console.WriteLine("\n----Selecting Data Using Dynamic LINQ----");
74+
75+
var selectStudents = students
76+
.Select("new {Name, Gender, np(House.Name, \"\") as House}")
77+
.ToDynamicList();
78+
79+
foreach (var student in selectStudents)
80+
{
81+
Console.WriteLine("{0}\t\t{1}\t{2}", student.Name, student.Gender, student.House);
82+
}
83+
84+
Console.WriteLine("\n");
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

Hogwards.Models/House.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Hogwards.Models
2+
{
3+
public class House
4+
{
5+
public long Id { get; set; }
6+
public string Name { get; set; }
7+
}
8+
}

Hogwards.Models/Student.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Hogwards.Models
2+
{
3+
public class Student
4+
{
5+
public Guid Id { get; set; }
6+
public string Name { get; set; }
7+
public int Age { get; set; }
8+
public string Gender { get; set; }
9+
public House House { get; set; }
10+
}
11+
}

0 commit comments

Comments
 (0)