예제 #1
0
        //左外联接
        public static void LeftOutJoinRun()
        {
            var racers = from r in Formulal.GetChampions()
                         from y in r.Years
                         select new
            {
                Year = y,
                Name = r.FirstName + " " + r.LastName
            };
            var teams = from t in Formulal.GetContructorChampions()
                        from y in t.Years
                        select new
            {
                Year = y,
                Name = t.Name
            };

            var racersAndTeams = (from r in racers
                                  join t in teams on r.Year equals t.Year into rt
                                  from t in rt.DefaultIfEmpty()
                                  orderby r.Year
                                  select new
            {
                r.Year,
                Champion = r.Name,
                Constructor = t == null ? "no constructor" : t.Name
            }).Take(10);

            Console.WriteLine("输出结果:");
            foreach (var item in racersAndTeams)
            {
                Console.WriteLine($"{item.Year}:{item.Champion,-10} {item.Constructor}");
            }
        }
예제 #2
0
        //内连接
        public static void InnerJoinRun()
        {
            var racers = from r in Formulal.GetChampions()
                         from y in r.Years
                         select new
            {
                Year = y,
                Name = r.FirstName + " " + r.LastName
            };
            var teams = from t in Formulal.GetContructorChampions()
                        from y in t.Years
                        select new
            {
                Year = y,
                Name = t.Name
            };

            var racersAndTeams = (from r in racers
                                  join t in teams on r.Year equals t.Year
                                  orderby t.Year
                                  select new
            {
                r.Year,
                Champion = r.Name,
                Constructor = t.Name
            }).Take(10);

            Console.WriteLine("输出结果:");
            foreach (var item in racersAndTeams)
            {
                Console.WriteLine($"{item.Year}:{item.Champion,-20} {item.Constructor}");
            }

            Console.WriteLine();
            Console.WriteLine(" 将上述合并为一个LINQ查询:");
            var racersAndTeams2 =
                (from r in from r1 in Formulal.GetChampions()
                 from yr in r1.Years
                 select new
            {
                Year = yr,
                Name = r1.FirstName + " " + r1.LastName
            }
                 join t in
                 from t1 in Formulal.GetContructorChampions()
                 from yt in t1.Years
                 select new
            {
                Year = yt,
                Name = t1.Name
            }
                 on r.Year equals t.Year
                 orderby t.Year
                 select new
            {
                Year = r.Year,
                Champion = r.Name,
                Constructor = t.Name
            }).Take(10);

            Console.WriteLine("输出结果:");
            foreach (var item in racersAndTeams2)
            {
                Console.WriteLine($"{item.Year}:{item.Champion,-20} {item.Constructor}");
            }
        }