Пример #1
0
        static void USeJoin()
        {
            var employeeRepository = new EmployeeRepository();

            var depatmentRepository = new DepartmentRepository();


            var query = from emp in employeeRepository.GetAll()
                        join d in depatmentRepository.GetAll()
                        on emp.DepartmentId equals d.ID
                        select new
            {
                Employee   = emp.Name,
                Department = d.Name
            };

            var query2 = from d in depatmentRepository.GetAll()
                         join emp in employeeRepository.GetAll()
                         on d.ID equals emp.DepartmentId
                         into gid
                         select new
            {
                Department = d.Name,
                Employee   = gid
            };



            foreach (var item in query2)
            {
                Console.WriteLine(item.Department);
                foreach (var item2 in item.Employee)
                {
                    Console.WriteLine(item2.Name);
                }
            }


            //foreach (var item in query)
            //{
            //    Console.WriteLine(item.Employee);
            //    Console.WriteLine(item.Department);
            //}
        }
Пример #2
0
        private static void UseJoin()
        {
            var employeesRepository  = new EmployeeRepository();
            var departmentRepository = new DepartmentRepository();

            var query =
                from d in departmentRepository.GetAll()
                join e in employeesRepository.GetAll()
                on
                d.ID equals e.DepartmentID
                into ed
                select new { Department = d.Name,
                             Employees  = ed };

            foreach (var group in query)
            {
                Console.WriteLine(group.Department);
                foreach (var employee in group.Employees)
                {
                    Console.WriteLine("\t" + employee.Name);
                }
            }
        }