当前位置: 首页>>代码示例>>C#>>正文


C# Department.Join方法代码示例

本文整理汇总了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);
        }
    }
开发者ID:MarKamenov,项目名称:TelerikAcademy,代码行数:67,代码来源:Program.cs


注:本文中的Department.Join方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。