Linq(3.连表、合并、分区(分页)、转换)

连表查询

使用join可以将两个数据源按照特定的条件连接到一起

内连接

查询一个年份列表,查询当前年份的赛车手冠军和车队冠军

            //得到所有冠军赛车手
            IList champions_racers = Formula1.GetChampions();
            //得到冠军车队
            IList champions_team = Formula1.GetContructorChampions();
            var query = from r in champions_racers
                        from c in r.Years
                        select new { 
                            year=c,
                            name=r.FirstName+" "+r.LastName
                        };
            var query1 = from r in champions_team
                        from c in r.Years
                        select new
                        {
                            year = c,
                            name = r.Name
                        };
            var query2 = (from r in query
                         join t in query1 on r.year equals t.year
                         orderby r.year
                         select new { r.year,rname=r.name,tname=t.name }).Take(10);
            foreach (var item in query2)
            {
                Console.WriteLine("year={0},rname={1},tname={2}", item.year,item.rname,item.tname);
            }

左连接

如赛车手比车队设立冠军的年份要早,可能某个年份只有赛车手冠军没有车队冠军,这时候需要左连接查询。

            //得到所有冠军赛车手
            IList champions_racers = Formula1.GetChampions();
            //得到冠军车队
            IList champions_team = Formula1.GetContructorChampions();
            var query = from r in champions_racers
                        from c in r.Years
                        select new { 
                            year=c,
                            name=r.FirstName+" "+r.LastName
                        };
            var query1 = from r in champions_team
                        from c in r.Years
                        select new
                        {
                            year = c,
                            name = r.Name
                        };
            var query2 = (from r in query
                         join t in query1 on r.year equals t.year into rt
                         from nrt in rt.DefaultIfEmpty()
                         orderby r.year
                          select new { r.year, rname = r.name, tname = nrt == null ? "No" : nrt.name }).Take(10);
            foreach (var item in query2)
            {
                Console.WriteLine("year={0},rname={1},tname={2}", item.year,item.rname,item.tname);
            }

使用into将结果放入新的临时表rt中,在使用rt.DefaultIfEmpty,返回一个新序列元素 nrt,赋值时判断nrt是否为空,既可。
linq只支持左连接,如要右连接,将query和query1调换位置

集合操作

可以使用扩展方法Intersect、Union、Distinct、Except等
如:查询同时拥有法拉利和迈凯伦获得冠军的车手。

            //得到所有冠军赛车手
            IList champions_racers = Formula1.GetChampions();
            //得到冠军车队
            IList champions_team = Formula1.GetContructorChampions();

            Func<string, IEnumerable> RacerByCar = carname => from cr in champions_racers
                                                                     from crcars in cr.Cars
                                                                     where crcars == carname
                                                                     orderby cr.LastName
                                                                     select cr;
            foreach (var item in RacerByCar("Ferrari").Except(RacerByCar("McLaren")))
            {
                Console.WriteLine(item.ToString("A"));
            }

合并

zip是.net4.0新增的,允许两个相关的数据源合并为一个

            var class1 = new List()
            {
                 new class1(){ name="Make"},
                 new class1(){ name="Phil"},
                 new class1(){ name="John"}
           };
            var class2 = new List()
                {
                    new class2(){ name="Vanwall", age=18},
                    new class2(){ name="Cooper", age=15},
                    new class2(){ name="Ferrari", age=28},
                    new class2(){ name="BRM", age=14},

                };
            var result = class1.Zip(class2, (r, t) => string.Format("class1-name={0},class2-name={1},age={2}", r.name, t.name, t.age));
            foreach (var item in result)
            {
                Console.WriteLine(item);
            }

Linq(3.连表、合并、分区(分页)、转换)_第1张图片

分区

skip和Task联合使用即可实现分页效果

            //得到所有冠军赛车手
            IList champions_racers = Formula1.GetChampions();
            //得到冠军车队
            IList champions_team = Formula1.GetContructorChampions();
            var query = (from cr in champions_racers select cr).Skip(1*5).Take(5);
            foreach (var item in query)
            {
                Console.WriteLine(item.ToString());
            }

转换

转换有聚合函数:sum 、count、min、max、average、aggregate等
转换操作符:ToList、ToLookup等等
生成操作符:Range、Empty等等

聚合

返回获得冠军超过3次的赛车手

            //得到所有冠军赛车手
            IList champions_racers = Formula1.GetChampions();
            //得到冠军车队
            IList champions_team = Formula1.GetContructorChampions();

            var query = from cr in champions_racers
                        let numberYears = cr.Years.Count()
                        where numberYears >= 3
                        orderby numberYears descending,cr.LastName
                        select new
                        {
                            name = cr.FirstName + " " + cr.LastName,
                            numberYears=numberYears
                        };
            foreach (var item in query)
            {
                Console.WriteLine(string.Format("{0}\t{1}",item.name,item.numberYears));
            }

使用 let 定义一个变量

转换操作符

ToList、ToLookup、ToArray等不做过多介绍

生成操作符

range、Repeat等等,使用很简单

foreach (var item in Enumerable.Range(1,20))
            {
                Console.WriteLine(item);
            }

你可能感兴趣的:(C#基础知识随笔记录)