public static void GetEmployeesByDepartment()
        {
            var employeesByDepartment = Department.GetAllDepartments()
                                        .GroupJoin(EmployeeWithDepartment.GetAllEmployees(),
                                                   d => d.ID,
                                                   e => e.DepartmentID,
                                                   (department, employees) => new
            {
                Department = department,
                Employees  = employees
            });

            var employeesByDepartment2 = from d in Department.GetAllDepartments()
                                         join e in EmployeeWithDepartment.GetAllEmployees()
                                         on d.ID equals e.DepartmentID into eGroup
                                         select new
            {
                Department = d,
                Employees  = eGroup
            };

            foreach (var department in employeesByDepartment)
            {
                Console.WriteLine(department.Department.Name);
                foreach (var employee in department.Employees)
                {
                    Console.WriteLine(" " + employee.Name);
                }
                Console.WriteLine();
            }
        }
Exemple #2
0
        public static void GetEmployeesByDepartment()
        {
            var result = from e in EmployeeWithDepartment.GetAllEmployees()
                         join d in Department.GetAllDepartments()
                         on e.DepartmentID equals d.ID into eGroup
                         from d in eGroup.DefaultIfEmpty()
                         select new
            {
                EmployeeName   = e.Name,
                DepartmentName = d == null ? "No Department" : d.Name
            };

            ///Rewrite Example 1 using extension method syntax.
            ///To implement Left Outer Join, with extension method syntax we use the GroupJoin() method along with SelectMany() and DefaultIfEmpty() methods.
            var result2 = EmployeeWithDepartment.GetAllEmployees()
                          .GroupJoin(Department.GetAllDepartments(),
                                     e => e.DepartmentID,
                                     d => d.ID,
                                     (emp, depts) => new { emp, depts })
                          .SelectMany(z => z.depts.DefaultIfEmpty(),
                                      (a, b) => new
            {
                EmployeeName   = a.emp.Name,
                DepartmentName = b == null ? "No Department" : b.Name
            });

            foreach (var v in result)
            {
                Console.WriteLine(v.EmployeeName + "\t" + v.DepartmentName);
            }
        }
Exemple #3
0
        public static void GetEmployeesByDepartment()
        {
            var result = EmployeeWithDepartment.GetAllEmployees().Join(Department.GetAllDepartments(),
                                                                       e => e.DepartmentID,
                                                                       d => d.ID, (employee, department) => new
            {
                EmployeeName   = employee.Name,
                DepartmentName = department.Name
            });

            ///Rewrite Example 1 using SQL like syntax.
            ///Notice that we are using the join operator but without into
            var result2 = from e in EmployeeWithDepartment.GetAllEmployees()
                          join d in Department.GetAllDepartments()
                          on e.DepartmentID equals d.ID
                          select new
            {
                EmployeeName   = e.Name,
                DepartmentName = d.Name
            };

            ///Notice that, in the output we don't have Andy record.
            ///This is because, Andy does not have a matching department in Department collection.
            foreach (var employee in result)
            {
                Console.WriteLine(employee.EmployeeName + "\t" + employee.DepartmentName);
            }
        }
Exemple #4
0
        public static void GetEmployeesByDepartment()
        {
            var result = from e in EmployeeWithDepartment.GetAllEmployees()
                         from d in Department.GetAllDepartments()
                         select new { e, d };

            ///Rewrite Example 1 using extension method syntax
            ///To implement Cross Join using extension method syntax, we could either use SelectMany() method or Join() method
            var result1_extend = Employee.GetAllEmployees()
                                 .SelectMany(e => Department.GetAllDepartments(), (e, d) => new { e, d });

            ///Implementing cross join using Join()
            var result1_extend2 = Employee.GetAllEmployees()
                                  .Join(Department.GetAllDepartments(),
                                        e => true,
                                        d => true,
                                        (e, d) => new { e, d });

            foreach (var v in result)
            {
                Console.WriteLine(v.e.Name + "\t" + v.d.Name);
            }

            ///In this case, every element from the Departments collection is combined with every element in the Employees collection.
            var result2 = from d in Department.GetAllDepartments()
                          from e in Employee.GetAllEmployees()
                          select new { e, d };

            foreach (var v in result2)
            {
                Console.WriteLine(v.e.Name + "\t" + v.d.Name);
            }
        }