-
Notifications
You must be signed in to change notification settings - Fork 3
/
01.03. SelectNewCreateObjects.linq
175 lines (159 loc) · 4.64 KB
/
01.03. SelectNewCreateObjects.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
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
<Query Kind="Program" />
void Main()
{
List<Person> people = new List<Person>()
{
new Person("Tod", "Vachev", 1, 180, 26, Gender.Male),
new Person("John", "Johnson", 2, 170, 21, Gender.Male),
new Person("Anna", "Maria", 3, 150, 22, Gender.Female),
new Person("Kyle", "Wilson", 4, 164, 29, Gender.Male),
new Person("Anna", "Williams", 5, 164, 28, Gender.Male),
new Person("Maria", "Ann", 6, 160, 43, Gender.Female),
new Person("John", "Jones", 7, 160, 37, Gender.Female),
new Person("Samba", "TheLion", 8, 175, 33, Gender.Male),
new Person("Aaron", "Myers", 9, 182, 21, Gender.Male),
new Person("Aby", "Wood", 10, 165, 20, Gender.Female),
new Person("Maddie","Lewis", 11, 160, 19, Gender.Female),
new Person("Lara", "Croft", 12, 162, 18, Gender.Female)
};
// 1. Creating new anonymous objects for Young People
var youngPeople = from p in people
where p.Age < 25
select new
{
FirstName = p.FirstName,
Age = p.Age
};
youngPeople.Dump();
// 2. Reusing the Properties Names
var youngPeopleSameProp = from p in people
where p.Age < 25
select new
{
p.FirstName,
p.Age
};
youngPeopleSameProp.Dump();
// 3. Reusing the Properties Names, Mix
var youngPeopleMixProp = from p in people
where p.Age < 25
select new
{
N = p.FirstName,
p.Age
};
youngPeopleMixProp.Dump();
// 4. Creating new YoungPerson Objects from Person
var youngPersonObj = from p in people
where p.Age < 25
select new YoungPerson
{
FullName = p.FirstName,
Age = p.Age
};
youngPersonObj.Dump();
// 5. Creating new YoungPerson Objects from Person
var youngPersonFullName = from p in people
where p.Age < 25
select new YoungPerson
{
FullName = string.Format($"{p.FirstName} {p.LastName}"),
Age = p.Age
};
youngPersonFullName.Dump();
}
// Define other methods and classes here
internal class YoungPerson
{
public string FullName { get; set; }
public int Age { get; set; }
}
internal class Person
{
private string firstName;
private string lastName;
private int id;
private int height;
private int age;
private Gender gender;
public string FirstName
{
get
{
return this.firstName;
}
set
{
this.firstName = value;
}
}
public string LastName
{
get
{
return this.lastName;
}
set
{
this.lastName = value;
}
}
public int ID
{
get
{
return this.id;
}
set
{
this.id = value;
}
}
public int Height
{
get
{
return this.height;
}
set
{
this.height = value;
}
}
public int Age
{
get
{
return this.age;
}
set
{
this.age = value;
}
}
public Gender Gender
{
get
{
return this.gender;
}
set
{
this.gender = value;
}
}
public Person(string firstName, string lastName, int id, int height, int age, Gender gender)
{
this.firstName = firstName;
this.lastName = lastName;
this.id = id;
this.Height = height;
this.Age = age;
this.Gender = gender;
}
}
internal enum Gender
{
Male,
Female
}