-
Notifications
You must be signed in to change notification settings - Fork 3
/
01.02. ObjectExamples.linq
112 lines (100 loc) · 2.73 KB
/
01.02. ObjectExamples.linq
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
<Query Kind="Program" />
void Main()
{
List<Person> people = new List<Person>()
{
new Person("Tod", 180, 70, Gender.Male),
new Person("John", 170, 88, Gender.Male),
new Person("Anna", 150, 48, Gender.Female),
new Person("Kyle", 164, 77, Gender.Male),
new Person("Anna", 164, 77, Gender.Male),
new Person("Maria", 160, 55, Gender.Female),
new Person("John", 160, 55, Gender.Female)
};
// 1. Linq Example with Objects
var fourCharPeople = from p in people
where (p.Name.Length == 4)
select p;
fourCharPeople.Dump();
// 2. Linq Example with Objects Condition and Ordering
var fourCharPeopleOrdered = from p in people
where (p.Name.Length == 4)
orderby p.Weight
select p;
fourCharPeopleOrdered.Dump();
// 3. Linq Example Extracting Properties from Objects in a new collection
var extractedNames = from p in people
where (p.Name.Length == 4)
select p.Name;
extractedNames.Dump();
// 4. Linq Example with Objects Condition and Special Ordering
var peopleSpecialOrder = from p in people
where (p.Name.Length == 4)
orderby p.Name, p.Height
select p;
peopleSpecialOrder.Dump();
}
// Define other methods and classes here
internal class Person
{
private string name;
private int height;
private int weight;
private Gender gender;
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
public int Height
{
get
{
return this.height;
}
set
{
this.height = value;
}
}
public int Weight
{
get
{
return this.weight;
}
set
{
this.weight = value;
}
}
public Gender Gender
{
get
{
return this.gender;
}
set
{
this.gender = value;
}
}
public Person(string name, int height, int weight, Gender gender)
{
this.Name = name;
this.Height = height;
this.Weight = weight;
this.Gender = gender;
}
}
internal enum Gender
{
Male,
Female
}