本文整理汇总了C#中Department.Join方法的典型用法代码示例。如果您正苦于以下问题:C# Department.Join方法的具体用法?C# Department.Join怎么用?C# Department.Join使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Department
的用法示例。
在下文中一共展示了Department.Join方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
private static void Main()
{
// Create students
var students = new Student[]
{
new Student("Nakov", "5018"),
new Student("Stephen Hawking", "5513"),
new Student("John Nash", "6012"),
new Student("Niki", "5018"),
new Student("Joro", "5018"),
new Student("Will Hunting", "6012"),
};
// Create departments
var departments = new Department[]
{
new Department("Mathematics", "6012"),
new Department("Physics", "5513"),
new Department("CompSci", "5018")
};
// Extract all students grouped by department (Task 18)
// Create a list of Department-Student pairs where
// each element is an anonymous type that contains a
// Student's name and the name of the Department where the Student is enrolled.
var studentsByDep =
from department in departments
join student in students on department.ID equals student.DepartmentID
select new { Department = department.Name, Student = student.Name };
// Same as above using extension methods (Task 19)
var query =
departments.Join(students,
department => department.ID,
student => student.DepartmentID,
(department, student) =>
new { Department = department.Name, Student = student.Name });
// Print the list (unsorted)
Console.WriteLine("{0, -15} : {1}", "Student Name", "Department");
Console.WriteLine("{0, -15} : {1}", "============", "==========");
foreach (var item in studentsByDep)
{
Console.WriteLine("{0, -15} : {1}", item.Student, item.Department);
}
// Sort by department and student name
var departmentGroups = studentsByDep.OrderBy(x => x.Department).ThenBy(x => x.Student);
Console.WriteLine("\n{0, -15} : {1}", "Department", "Student Name");
Console.WriteLine("{0, -15} : {1}", "==========", "============");
foreach (var item in departmentGroups)
{
Console.WriteLine("{0, -15} : {1}", item.Department, item.Student);
}
// Only the math students (Task 16)
var mathStudents = studentsByDep.Where(x => x.Department == "Mathematics").OrderBy(x => x.Student);
Console.WriteLine();
Console.WriteLine("\nMathematics");
Console.WriteLine("===========");
foreach (var item in mathStudents)
{
Console.WriteLine(item.Student);
}
}