-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter19.cs
More file actions
320 lines (258 loc) · 11.6 KB
/
Chapter19.cs
File metadata and controls
320 lines (258 loc) · 11.6 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
using System;
using System.Collections;
using System.Linq;
using System.Xml.Linq;
namespace Chapter19
{
//C#不能直接在命名空间中直接声明变量或函数
class Program
{
static void MainTest()
{
int[] numbers = {2,12,5,15};
IEnumerable lowNums =
from n in numbers
where n < 10
select n;
foreach(var x in lowNums)
{
Console.Write("{0}, ", x);
}
Console.WriteLine();
Console.WriteLine("************************************");
var student = new {Name = "Mary Jones", Age = 19, Major = "History"};
Console.WriteLine("{0}, Age {1}, Major: {2}", student.Name, student.Age, student.Major);
Console.WriteLine("*************************************");
int[] numbers1 = {2, 5, 28, 31, 17, 16, 42};
var numsQuery = from n in numbers1
where n < 20
select n;
var numsMethod = numbers1.Where(x => x < 20);
int numsCount = (from n in numbers
where n < 20
select n).Count();
foreach(var x in numsQuery)
Console.Write("{0}, ", x);
Console.WriteLine();
foreach(var x in numsMethod)
Console.Write("{0}, ", x);
Console.WriteLine();
Console.WriteLine(numsCount);
Console.WriteLine("*************************************************");
var query = from s in students
join c in studentsInCourses on s.StID equals c.StID
where c.CourseName == "History"
select s.LastName;
foreach(var q in query)
Console.WriteLine("Student taking Histroy: {0}", q);
Console.WriteLine("**************************************************");
var groupA = new[]{3,4,5,6};
var groupB = new[]{6,7,8,9};
var someInts = from a in groupA
from b in groupB
where a > 4 && b <=8
select new{a,b, sum = a + b};
foreach(var a in someInts)
Console.WriteLine(a);
Console.WriteLine("**************************************************");
var someInts1 = from a in groupA
from b in groupB
let sum = a + b
where sum == 12
select new{a, b, sum};
foreach(var a in someInts1)
Console.WriteLine(a);
Console.WriteLine("**************************************************");
var students1 = new[]
{
new{LName = "Jones", FName = "Mary", Age = 19, Major = "History"},
new{LName = "Smith", FName = "Bob", Age = 20, Major = "CompSci"},
new{LName = "Fleming", FName = "Carol", Age = 21, Major = "History"}
};
var query1 = from student1 in students1
orderby student1.Age
select student1;
foreach(var s1 in query1)
{
Console.WriteLine("{0}, {1}, {2}, {3}",
s1.LName, s1.FName, s1.Age, s1.Major );
}
Console.WriteLine("***************************************");
var query2 = from student2 in students1
group student2 by student2.Major;
foreach(var s in query2)
{
Console.WriteLine("{0}", s.Key);
foreach(var t in s)
Console.WriteLine(" {0}, {1}", t.LName, t.FName);
}
Console.WriteLine("*****************************************");
var groupC = new[]{3,4,5,6};
var groupD = new[]{4,5,6,7};
var someInts8 = from c in groupC
join d in groupD on c equals d
into groupCandD
from x in groupCandD
select x;
foreach(var y in someInts8)
Console.Write("{0} ", y);
Console.WriteLine();
Console.WriteLine("****************************************");
int[] numbersX = new int[] {2,4,6};
int total = numbersX.Sum();
int howMany = numbersX.Count();
Console.WriteLine("Total: {0}, Count: {1}", total, howMany);
Console.WriteLine("*****************************************");
XDocument employees1 =
new XDocument(
new XElement("Employees",
new XElement("Name", "Bob Smith"),
new XElement("Name", "Sally Jones")
)
);
//employees1.Save("EmployeesFile.xml");
XDocument employees2 = XDocument.Load("C:\\Users\\e823551\\Documents\\MyC#Learning\\EmployeesFile.xml");
Console.WriteLine(employees2);
Console.WriteLine("************************************************");
XDocument employeeDoc =
new XDocument(
new XElement("Employees",
new XElement("Employee",
new XElement("Name", "Bob Simith"),
new XElement("PhoneNumber", "408-555-1000")),
new XElement("Employee",
new XElement("Name", "Sally Jones"),
new XElement("PhoneNumber", "415-555-2000"),
new XElement("PhoneNumber", "415-555-2001"))
)
);
//Console.WriteLine(employeeDoc);
XElement root = employeeDoc.Element("Employees");
IEnumerable employees = root.Elements();
foreach(XElement emp in employees)
{
XElement empNameNode = emp.Element("Name");
Console.WriteLine(empNameNode.Value);
IEnumerable empPhones = emp.Elements("PhoneNumber");
foreach(XElement phone in empPhones)
Console.WriteLine(" {0}",phone.Value);
}
Console.WriteLine("********************************************");
XDocument xd = new XDocument
(
new XElement("root",
new XElement("first")
)
);
Console.WriteLine("Original tree");
Console.WriteLine(xd);
Console.WriteLine();
XElement rt = xd.Element("root");
rt.Add(new XElement("Second"));
rt.Add(new XElement("third"),
new XComment("Important Comment"),
new XElement("fourth"));
Console.WriteLine("Modified tree");
Console.WriteLine(xd);
Console.WriteLine("***************************************");
XDocument xd1 = new XDocument
(
new XElement("root",
new XAttribute("color", "red"),
new XAttribute("size", "large"),
new XElement("first"),
new XElement("second")
)
);
Console.WriteLine(xd1);
Console.WriteLine();
XElement rt1 = xd1.Element("root");
XAttribute color = rt1.Attribute("color");
XAttribute size = rt1.Attribute("size");
Console.WriteLine("color is {0}", color.Value);
Console.WriteLine("size is {0}", size.Value);
rt1.Attribute("color").Remove(); //移除color特性
rt1.SetAttributeValue("size", null); //移除size特性
Console.WriteLine(xd1);
Console.WriteLine();
rt1.SetAttributeValue("size", "medium");
rt1.SetAttributeValue("width", "narrow");
Console.WriteLine(rt1);
Console.WriteLine();
Console.WriteLine("*************************************************");
XDocument xd2 = new XDocument
(
new XDeclaration("1.0", "utf-8", "yes"),
new XComment("This is a comment"),
new XProcessingInstruction("xml-stylesheet", @"href=""stories-css"" type=""text/css"""),
new XElement("root",
new XElement("first"),
new XElement("second")
)
);
Console.WriteLine(xd2);
Console.WriteLine("*************************************************");
XDocument xd3 = new XDocument(
new XElement("MyElements",
new XElement("first",
new XAttribute("color", "red"),
new XAttribute("size", "small")
),
new XElement("second",
new XAttribute("color", "red"),
new XAttribute("size", "medium")
),
new XElement("third",
new XAttribute("color", "blue"),
new XAttribute("size", "large")
)
)
);
Console.WriteLine(xd3);
XElement rt3 = xd3.Element("MyElements");
var xyz = from e in rt3.Elements()
where e.Name.ToString().Length == 5
select e;
foreach(XElement x in xyz)
Console.WriteLine(x.Name.ToString());
Console.WriteLine();
foreach(XElement x in xyz)
Console.WriteLine("Name: {0}, color: {1}, size: {2}",
x.Name,
x.Attribute("color").Value,
x.Attribute("size").Value);
Console.WriteLine("************************************************");
var xyz1 = from e in rt3.Elements()
select new{e.Name, color = e.Attribute("color")};
foreach(var x in xyz1)
Console.WriteLine(x);
Console.WriteLine();
foreach(var x in xyz1)
Console.WriteLine("{0,-6}, color:{1,-7}",x.Name, x.color.Value);
}
public class Student
{
public int StID;
public string LastName;
}
public class CourseStudent
{
public string CourseName;
public int StID;
}
static Student[] students = new Student[]
{
new Student{StID = 1, LastName = "Carson"},
new Student{StID = 2, LastName = "Klassen"},
new Student{StID = 3, LastName = "Fleming"}
};
static CourseStudent[] studentsInCourses = new CourseStudent[]
{
new CourseStudent{CourseName = "Art", StID = 1},
new CourseStudent{CourseName = "Art", StID = 2},
new CourseStudent{CourseName = "History", StID = 1},
new CourseStudent{CourseName = "History", StID = 3},
new CourseStudent{CourseName = "Physics", StID = 3}
};
}
}