示例#1
0
 public static void Main(string[] args)
 {
     var querySyntax = from student in Student.GetAllStudents()
                       join address in Address.GetAllAddresses()
                       on student.AddressId equals address.Id
                       join department in Department.GetAllDepartments()
                       on student.DepartmentId equals department.Id
                       select new
                       {
                           StudentName = student.Name,
                           Department = department.Name,
                           Road = address.Street
                       };
     foreach (var student in querySyntax)
     {
         Console.WriteLine(student.StudentName + " " + student.Department + " " + student.Road);
     }
 }
示例#2
0
文件: Join.cs 项目: jabihad/C-Sharp
        public static void Main(string[] args)
        {
            // Linq's Join is equivalent to SQL's Inner Join
            // Linq's GroupJoin is equivalent to SQL's Outer Join
            // Left Outer Join and Right Outer Join can be performed by exchanging data source
            // Cross Join doesn't require common property. It generates cartesian products of the collection

            var joinQuerySyntax = from dept in Department.GetAllDepartments()
                                  join std in Student.GetAllStudents()
                                  on dept.ID equals std.DepartmentId
                                  select new { Name = std.Name, Department = dept.Name };
            //foreach(var std in joinQuerySyntax)
            //{
            //    Console.WriteLine(std.Name + " " + std.Department);
            //}

            var groupJoinQuerySyntax = from dept in Department.GetAllDepartments()
                                       join std in Student.GetAllStudents()
                                       on dept.ID equals std.DepartmentId
                                       into StudentGroups         // List of children. Here it is list of Student
                                       select new { Department = dept.Name, StudentGroups = StudentGroups };
            // from p in Parent
            // join c in Child
            // on p.Id equals c.Id into GROUPJOIN
            // select new { NewParent = Parent, NewChildren = GROUPJOIN};
            // NewChildren is a list of Children. It can be empty if there is no children

            //Outer Foreach is for all department
            //foreach (var item in groupJoinQuerySyntax)
            //{
            //    Console.WriteLine("Department :" + item.Department);
            //    //Inner Foreach loop for each student of a department
            //    foreach (var student in item.StudentGroups)
            //    {
            //        Console.WriteLine("  StudentID : " + student.Id + " , Name : " + student.Name);
            //    }
            //}
            var flattenedQuerySyntax = from dept in Department.GetAllDepartments()
                                       join std in Student.GetAllStudents()
                                       on dept.ID equals std.DepartmentId
                                       into StudentGroups
                                       from student in StudentGroups.DefaultIfEmpty()
                                       select new { Department = dept, StudentName = student?.Name };

            //foreach(var item in flattenedQuerySyntax)
            //{
            //Console.WriteLine("Department - " + item.Department.Name + " . Student - " + item.StudentName);
            //}



            var crossJoin = from std in Student.GetAllStudents()
                            from dept in Department.GetAllDepartments()
                            select new
            {
                Name      = std.Name,
                Depatment = dept.Name
            };

            Console.WriteLine(crossJoin.LongCount()); // Number of Student * Number of Department
        }