コード例 #1
0
        private static void CombineRacers()
        {
            var q = from r in Formula1.GetChampions()
                    join r2 in Formula1.GetChampionships().GetRacerInfo() on
                    new
            {
                FirstName = r.FirstName,
                LastName  = r.LastName
            }
            equals
            new
            {
                FirstName = r2.FirstName,
                LastName  = r2.LastName
            }
            into yearResults
                select new
            {
                FirstName = r.FirstName,
                LastName  = r.LastName,
                Wins      = r.Wins,
                Starts    = r.Starts,
                Results   = yearResults
            };

            foreach (var item in q)
            {
                Console.WriteLine("{0} {1}", item.FirstName, item.LastName);
                foreach (var item2 in item.Results)
                {
                    Console.WriteLine("{0} {1}", item2.Year, item2.Position);
                }
            }
        }
コード例 #2
0
        private static void SelectMany2()
        {
            // flatten the year list to return a list of all racers and positions in the championship
            var racers = Formula1.GetChampionships()
                         .SelectMany(cs => new List <dynamic>()
            {
                new {
                    Year     = cs.Year,
                    Position = 1,
                    Name     = cs.First
                },
                new {
                    Year     = cs.Year,
                    Position = 2,
                    Name     = cs.Second
                },
                new {
                    Year     = cs.Year,
                    Position = 3,
                    Name     = cs.Third
                }
            });


            foreach (var s in racers)
            {
                Console.WriteLine(s);
            }
        }
コード例 #3
0
        static void GroupJoin2()
        {
            var racers = Formula1.GetChampionships()
                         .SelectMany(cs => new List <RacerInfo>()
            {
                new RacerInfo {
                    Year      = cs.Year,
                    Position  = 1,
                    FirstName = cs.First.FirstName(),
                    LastName  = cs.First.LastName()
                },
                new RacerInfo {
                    Year      = cs.Year,
                    Position  = 2,
                    FirstName = cs.Second.FirstName(),
                    LastName  = cs.Second.LastName()
                },
                new RacerInfo {
                    Year      = cs.Year,
                    Position  = 3,
                    FirstName = cs.Third.FirstName(),
                    LastName  = cs.Third.LastName()
                }
            });

            var q = (from r in Formula1.GetChampions()
                     join r2 in racers on
                     new
            {
                FirstName = r.FirstName,
                LastName = r.LastName
            }
                     equals
                     new
            {
                FirstName = r2.FirstName,
                LastName = r2.LastName
            }
                     into yearResults
                     select new
            {
                FirstName = r.FirstName,
                LastName = r.LastName,
                Wins = r.Wins,
                Starts = r.Starts,
                Results = yearResults
            });

            foreach (var r in q)
            {
                Console.WriteLine("{0} {1}", r.FirstName, r.LastName);
                foreach (var results in r.Results)
                {
                    Console.WriteLine("{0} {1}", results.Year, results.Position);
                }
            }
        }
コード例 #4
0
        private static void Except()
        {
            var racers = Formula1.GetChampionships().SelectMany(cs => new List <RacerInfo>()
            {
                new RacerInfo {
                    Year      = cs.Year,
                    Position  = 1,
                    FirstName = cs.First.FirstName(),
                    LastName  = cs.First.LastName()
                },
                new RacerInfo {
                    Year      = cs.Year,
                    Position  = 2,
                    FirstName = cs.Second.FirstName(),
                    LastName  = cs.Second.LastName()
                },
                new RacerInfo {
                    Year      = cs.Year,
                    Position  = 3,
                    FirstName = cs.Third.FirstName(),
                    LastName  = cs.Third.LastName()
                }
            });


            var nonChampions = racers.Select(r =>
                                             new
            {
                FirstName = r.FirstName,
                LastName  = r.LastName
            }).Except(Formula1.GetChampions().Select(r =>
                                                     new
            {
                FirstName = r.FirstName,
                LastName  = r.LastName
            }));

            foreach (var r in nonChampions)
            {
                Console.WriteLine("{0} {1}", r.FirstName, r.LastName);
            }
        }