LINQ相关的集合操作

来源:互联网 发布:python绝技 pdf 编辑:程序博客网 时间:2024/06/05 12:00

摘选自C#高级编程(第9版) --C# 5.0 & .NET 4.5.1

Code:

      static void Main(string[] args)        {            LinqQuery();            object[] data = { "one", 2, 3, "four", "five", 6 };            var q = data.OfType<string>();//OfType使用            //多个from,实际是转为SelectMany()扩展方法            var ferraiDrivers = from r in Formula1.GetChampions()                                from c in r.Cars                                where c == "Ferrari"                                orderby r.LastName                                select r.FirstName + " " + r.LastName;            //对应扩展方法为            var ferraiDrivers2 = Formula1.GetChampions()                .SelectMany(r => r.Cars, (r, c) => new { Racer = r, Car = c })                .Where(r => r.Car == "Ferrari")                .OrderBy(r => r.Racer.LastName).Select(r => r.Racer.FirstName + " " + r.Racer.LastName);            foreach (var s in ferraiDrivers)                Console.WriteLine(s);            //join            var racersAndTeams = (from r in                                      from r1 in Formula1.GetChampions()                                      from yr in r1.Years                                      select new                                      {                                          Year = yr,                                          Name = r1.FirstName + " " + r1.LastName                                      }                                  join t in                                    from t1 in Formula1.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,                                      Racer = r.Name,                                      Team = t.Name                                  }).Take(10);            foreach (var item in racersAndTeams)            {                Console.WriteLine("{0}: {1,-20} {2}", item.Year, item.Racer, item.Team);            }            //Intersect操作            Func<string, IEnumerable<Racer>> racersByCar =                car => from r in Formula1.GetChampions()                       from c in r.Cars                       where c == car                       orderby r.LastName                       select r;            Console.WriteLine("World champion with Ferrari and McLaren");            foreach (var racer in racersByCar("Ferrari").Intersect(racersByCar("McLaren")))            {                Console.WriteLine(racer);            }            //Zip(合并)            Console.WriteLine("Zip:");            var racerNames = from r in Formula1.GetChampions()                             where r.Country == "Itally"                             orderby r.Wins descending                             select new                             {                                 Name = r.FirstName + " " + r.LastName                             };            var racerNamesAndStarts = from r in Formula1.GetChampions()                                      where r.Country == "Itally"                                      orderby r.Wins descending                                      select new                                      {                                          LastName = r.LastName,                                          Starts = r.Starts                                      };            var racers = racerNames.Zip(racerNamesAndStarts, (a, b) => a.Name + ",Starts:" + b.Starts);            foreach (var r in racers)                Console.WriteLine(r);            //count sum            var query = from r in Formula1.GetChampions()                        let nYears = r.Years.Count()                        where nYears >= 3                        orderby nYears descending, r.LastName                        select new                        {                            Name = r.FirstName + " " + r.LastName,                            TimesChampion = nYears                        };            foreach (var r in query)                Console.WriteLine("{0} {1}", r.Name, r.TimesChampion);            var countries = (from c in                                 from r in Formula1.GetChampions()                                 group r by r.Country into c                                 select new                                 {                                     Country = c.Key,                                     Wins = (from r1 in c select r1.Wins).Sum()                                 }                             orderby c.Wins descending, c.Country                             select c).Take(5);            foreach (var c in countries)                Console.WriteLine("{0} {1}", c.Country, c.Wins);            //非类型化的集合操作            var l = new ArrayList(Formula1.GetChampions() as ICollection);            var query1 = from r in l.Cast<Racer>()                         where r.Country == "USA"                         orderby r.Wins descending                         select r;            foreach (var r in query1)                Console.WriteLine("{0:A}", r);            Console.ReadKey();        }

        private static void LinqQuery()        {            var q = from r in Formula1.GetChampions()                    where r.Country == "Brazil"                    orderby r.Wins descending                    select r;            //使用扩展方法            var racers = Formula1.GetChampions()                 .Where((r, index) => r.LastName.StartsWith("A") && index % 2 != 0);            foreach (Racer r in racers)            {                Console.WriteLine("{0:A}", r);            }        }


注: 相关实体类见书



0 0
原创粉丝点击