Trong phần này, bạn sẽ tìm hiểu một số truy vấn LINQ phức tạp. Chúng tôi sẽ sử dụng danh sách Student và Standard dưới đây cho các truy vấn của chúng tôi.
IList<Student> studentList = new List<Student>()
{
new Student() { StudentID = 1, StudentName = "John", Age = 18, StandardID = 1 },
new Student() { StudentID = 2, StudentName = "Steve", Age = 21, StandardID = 1 },
new Student() { StudentID = 3, StudentName = "Bill", Age = 18, StandardID = 2 },
new Student() { StudentID = 4, StudentName = "Ram", Age = 20, StandardID = 2 },
new Student() { StudentID = 5, StudentName = "Ron", Age = 21 }
};
IList<Standard> standardList = new List<Standard>()
{
new Standard(){ StandardID = 1, StandardName="Standard 1"},
new Standard(){ StandardID = 2, StandardName="Standard 2"},
new Standard(){ StandardID = 3, StandardName="Standard 3"}
};
var studentNames = studentList.Where(s => s.Age > 18)
.Select(s => s)
.Where(st => st.StandardID > 0)
.Select(s => s.StudentName);
Đây là kết quả khi biên dịch và thực thi:
Steve
Ram
Truy vấn sau đây trả về danh sách các đối tượng ẩn danh chỉ có thuộc tính StudentName:
var teenStudentsName = from s in studentList
where s.age > 12 && s.age < 20
select new { StudentName = s.StudentName };
teenStudentsName.ToList().ForEach(s => Console.WriteLine(s.StudentName));
Đây là kết quả khi biên dịch và thực thi:
John
Bill
Truy vấn sau đây trả về danh sách nhóm sinh viên theo StandardID:
var studentsGroupByStandard = from s in studentList
group s by s.StandardID into sg
orderby sg.Key
select new { sg.Key, sg };
foreach (var group in studentsGroupByStandard)
{
Console.WriteLine("StandardID {0}:", group.Key);
group.sg.ToList().ForEach(st => Console.WriteLine(st.StudentName ));
}
Đây là kết quả khi biên dịch và thực thi:
StandardID 0:
Ron
StandardID 1:
John
Steve
StandardID 2:
Bill
Ram
Đầu ra bao gồm Ron, người không có StandardID. Vì vậy, Ron mặc định có StandardID 0.
Để xóa một học sinh không có StandardID, hãy sử dụng toán tử where trước toán tử nhóm:
var studentsGroupByStandard = from s in studentList
where s.StandardID > 0
group s by s.StandardID into sg
orderby sg.Key
select new { sg.Key, sg };
Đây là kết quả khi biên dịch và thực thi:
StandardID 1:
John
Steve
StandardID 2:
Bill
Ram
Sử dụng Left Join để hiển thị Student theo từng Standard. Hiển thị tên Standard ngay cả khi không có Student nào được gán cho Standard đó.
var studentsGroup = from stad in standardList
join s in studentList
on stad.StandardID equals s.StandardID
into sg
select new
{
StandardName = stad.StandardName,
Students = sg
};
foreach (var group in studentsGroup)
{
Console.WriteLine(group.StandardName);
group.Students.ToList().ForEach(st => Console.WriteLine(st.StudentName));
}
Đây là kết quả khi biên dịch và thực thi:
Standard 1
John
Steve
Standard 2
Bill
Ram
Standard 3
Trong ví dụ sau chúng tôi trả về danh sách bao gồm StudentName và StandardName tương ứng:
var studentsWithStandard = from stad in standardList
join s in studentList
on stad.StandardID equals s.StandardID
into sg
from std_grp in sg
orderby stad.StandardName, std_grp.StudentName
select new
{
StudentName = std_grp.StudentName,
StandardName = stad.StandardName
};
foreach (var group in studentsWithStandard)
{
Console.WriteLine("{0} is in {1}", group.StudentName, group.StandardName);
}
Đây là kết quả khi biên dịch và thực thi:
John is in Standard 1
Steve is in Standard 1
Bill is in Standard 2
Ram is in Standard 2
Truy vấn sau đây trả về danh sách Student theo thứ tự tăng dần của StandardID và Age.
var sortedStudents = from s in studentList
orderby s.StandardID, s.Age
select new
{
StudentName = s.StudentName,
Age = s.Age,
StandardID = s.StandardID
};
sortedStudents.ToList().ForEach(s =>
Console.WriteLine("Student Name: {0}, Age: {1}, StandardID: {2}",
s.StudentName, s.Age , s.StandardID));
Đây là kết quả khi biên dịch và thực thi:
Student Name: Ron, Age: 21, StandardID: 0
Student Name: John, Age: 18, StandardID: 1
Student Name: Steve, Age: 21, StandardID: 1
Student Name: Bill, Age: 18, StandardID: 2
Student Name: Ram, Age: 20, StandardID: 2
var studentWithStandard = from s in studentList
join stad in standardList
on s.StandardID equals stad.StandardID
select new
{
StudentName = s.StudentName,
StandardName = stad.StandardName
};
studentWithStandard.ToList().ForEach(s =>
Console.WriteLine("{0} is in {1}", s.StudentName, s.StandardName ));
Đây là kết quả khi biên dịch và thực thi:
John is in Standard 1
Steve is in Standard 1
Bill is in Standard 2
Ram is in Standard 2
var nestedQueries = from s in studentList
where s.age > 18 && s.StandardID ==
(from std in standardList
where std.StandardName == "Standard 1"
select std.StandardID).FirstOrDefault()
select s;
nestedQueries.ToList().ForEach(s => Console.WriteLine(s.StudentName));
Đây là kết quả khi biên dịch và thực thi:
Steve
Bạn có thể vui lòng tắt trình chặn quảng cáo ❤️ để hỗ trợ chúng tôi duy trì hoạt động của trang web.
Từ khóa let, into trong LINQ có tác dụng gì? Hướng dẫn khai báo và sử dụng từ khóa let, into trong LINQ.
Trì hoãn thực thi truy vấn LINQ là gì? Thực thi ngay lập tức truy vấn LINQ là gì? Làm sao để thực thi truy vấn LINQ.
Expression trong LINQ là gì? Cây biểu thức trong LINQ là gì? Cách khai báo và sử dụng chúng trong LINQ.
Toán tử chuyển đổi trong LINQ là gì? Làm sao để khai báo và sử dụng toán tử chuyển đổi trong LINQ.