static void Main1()
        {
            //Sql Like Syntax :
            var result1 = from e in Employee5.GetAllEmployees()
                          join d in Department1.GetAllDepartments()
                          on e.DepartmentId equals d.Id into eGroup
                          from dept in eGroup.DefaultIfEmpty()
                          select new
            {
                EmployeeName   = e.Name,
                DepartmentName = dept == null ? "No Department" : dept.Name,
            };

            //Extension Method Syntax: Here we Use GroupJoin to Perform the Left Join
            var result2 = Employee5.GetAllEmployees()
                          .GroupJoin(Department1.GetAllDepartments(),
                                     emp => emp.DepartmentId,
                                     dept => dept.Id,
                                     (emp, depts) => new
            {
                emp,
                depts
            })
                          .SelectMany(z => z.depts.DefaultIfEmpty(),
                                      (a, department) => new
            {
                EmployeeName   = a.emp.Name,
                DepartmentName = department == null ? "Not Having Department" : department.Name
            });

            foreach (var employee in result1)
            {
                Console.WriteLine(employee.EmployeeName + "\t\t\t" + employee.DepartmentName);
            }
        }
        static void Main1()
        {
            //Here we wanna to get All the Information about an employee
            //Linq Syntax:
            var employees1 = Employee5.GetAllEmployees()
                             .Join(Department1.GetAllDepartments(),
                                   emp => emp.DepartmentId,
                                   dept => dept.Id,
                                   (employee, department) => new            //Here we get a Combnation of the Employee and Department as result of Join
            {
                EmployeeName   = employee.Name,
                DepartmentName = department.Name                                //In the End we get IEnumerable<AnonymousType(Employee)>
            });
            //Sql Like Syntax:
            var employees2 = from emp in Employee5.GetAllEmployees()
                             join dept in Department1.GetAllDepartments()
                             on emp.DepartmentId equals dept.Id
                             select new
            {
                EmployeeName   = emp.Name,
                DepartmentName = dept.Name
            };

            //Now We have to Loop throug all the Anonymous Type in the result Collection
            foreach (var employee in employees2)
            {
                Console.WriteLine(employee.EmployeeName + "\t\t" + employee.DepartmentName);
            }
        }
示例#3
0
        static void Main()
        {
            //Sql Like Syntax:
            var result1 = from e in Employee5.GetAllEmployees()
                          from d in Department1.GetAllDepartments()
                          select new
            {
                e,
                d
            };
            //By Using SelectMany Opeartor or Extension Method
            var result2 = Employee5.GetAllEmployees().SelectMany(e => Department1.GetAllDepartments(),          //This will associate each employee with all the Departments
                                                                 (e, d) => new { e, d });                       //Now we have a List<Emp,Dep> in a Cross Join Format

            //By using Join Extension Method:
            var result3 = Employee5.GetAllEmployees().Join(Department1.GetAllDepartments(),
                                                           e => true,
                                                           d => true,
                                                           (e, d) => new { e, d });

            Console.WriteLine("Total Count = " + result3.Count());
            foreach (var combo in result3)
            {
                Console.WriteLine(combo.e.Name + "\t\t\t" + combo.d.Name);
            }
            Console.WriteLine("/////////////////////////////////////");
        }
        static void Main1()
        {
            //Linq Syntax For GroupJoin:
            var employeesByDepartment1 = Department1.GetAllDepartments()
                                         .GroupJoin(Employee5.GetAllEmployees(),
                                                    d => d.Id,
                                                    e => e.DepartmentId,
                                                    (department, employees) => new
            {
                Department = department,
                Employees  = employees.OrderBy(e => e.Name)
            });

            //Sql Like Syntax For GroupJoin:
            var employeesByDepartment2 = from d in Department1.GetAllDepartments()
                                         join e in Employee5.GetAllEmployees()
                                         on d.Id equals e.DepartmentId into eGroup          //Please note: Group Join uses the join operator and the into keyword to group the results of the join.
                                         select new
            {
                Department = d,
                Employees  = eGroup.OrderBy(e => e.Name)
            };

            //It Something Like a List<Group> or List<Department having Employeess>
            foreach (var department in employeesByDepartment2)
            {
                Console.WriteLine(department.Department.Name);
                foreach (var employee in department.Employees)
                {
                    Console.WriteLine(" " + employee.Name);
                }
                Console.WriteLine();
            }
        }