-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
84 lines (61 loc) · 2.43 KB
/
Program.cs
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
using Hogwards.Models;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
var studentData = new StudentData();
var students = studentData.GetStudents();
//House.Name != null && House.Name == "RavenClaw"
Console.WriteLine("----Where condition on property under a sub object----");
Console.WriteLine("----House.Name == RavenClaw----");
Console.WriteLine("----Not null check using np() function----");
var studentByHouseResult = students
.Where("np(House.Name) == \"Ravenclaw\"")
.ToList();
foreach (var student in studentByHouseResult)
{
Console.WriteLine($"{student.Name}\t\t{student.House?.Name}\t{student.Age}");
}
//Dynamically creating lambda expressions
Console.WriteLine("\n----Dynamic Lambda----");
Console.WriteLine("----Students older than 14----");
Expression<Func<Student, bool>> itExpression = DynamicExpressionParser
.ParseLambda<Student, bool>(new ParsingConfig(), true, "House.Name == @0", "Hufflepuff");
Expression<Func<Student, bool>> ageExpression = DynamicExpressionParser
.ParseLambda<Student, bool>(new ParsingConfig(), true, "Age >= @0", 14);
var conditionCheck = "age";
var lambdaResult = students
.Where("@0(it)", conditionCheck == "house" ? itExpression : ageExpression)
.ToList();
foreach (var student in lambdaResult)
{
Console.WriteLine($"{student.Name}\t\t{student.House?.Name}\t{student.Age}");
}
//Group sort
Console.WriteLine("\n----Group Sort----");
Console.WriteLine("----Sort by house by descending order, then sort the students starting from elder to younger under each house----");
var groupSortResult = students
.OrderBy("np(House.Name, \"\") asc, age desc")
.ToList();
foreach (var student in groupSortResult)
{
Console.WriteLine($"{student.Name}\t\t{student.House?.Name}\t{student.Age}");
}
//Selecting Data Using Dynamic LINQ
//public List<Student> GetStudents() =>
// students
// .Select(s => new Student
// {
// Name = s.Name,
// age = s.Age,
// House = s.House.House,
// })
// .ToList();
Console.WriteLine("\n----Selecting Data Using Dynamic LINQ----");
var selectStudents = students
.Select("new {Name, Gender, np(House.Name, \"\") as House}")
.ToDynamicList();
foreach (var student in selectStudents)
{
Console.WriteLine($"{student.Name}\t\t{student.Gender}\t{student.House}");
}
Console.WriteLine("\n");