Linq的基础2

var 构建匿名类型1 = from c in ctx.GetTable<Customers>()
                          select new
                          {
                              城市 = c.City,
                              名字 = c.Name
                          };

var 构建匿名类型2 = from emp in ctx.GetTable<Customers>()
                          select new
                          {
                              城市 = emp.City,
                              名字 = emp.Name
                          };

var 多条件 = from c in ctx.GetTable<Customers>()
                      where c.Name == "Jay" && c.City == "雷州"
                      select new
                      {
                          名字 = c.City,
                          城市 = c.City
                      };

var 排序 = from c in ctx.GetTable<Customers>()
                     where c.City == "雷州"
                     orderby c.Name descending, c.City ascending
                     select new
                     {
                         名字 = c.Name,
                         城市 = c.City
                     };

//按照每页 10 条记录,查询第二页的顾客
            var 分页 = (from c in ctx.GetTable<Customers>() select c).Skip(10).Take(10);

//根据顾客的国家分组,查询顾客数大于 5 的国家名和顾客数
            var 一般分组 = from c in ctx.GetTable<Customers>()
                       group c by c.City into g
                       where g.Count() > 5
                       orderby g.Count() descending
                       select new
                       {
                           国家=g.Key,
                           顾客数=g.Count()
                       };

//根据国家和城市分组,查询顾客覆盖的国家和城市
            var 匿名类型分组 = from c in ctx.GetTable<Customers>()
                         group c by new { c.City, c.Name } into g
                         orderby g.Key.City, g.Key.Name
                         select new
                         {
                             国家 = g.Key.Name,
                             城市 = g.Key.City
                         };

var 过滤相同项 = (from c in ctx.GetTable<Customers>() orderby c.Name select c.Name).Distinct();

var 连接并且过滤相同项 = (from c in ctx.GetTable<Customers>()
                             where c.City.Contains("A")
                             select c)
                           .Union(from c in ctx.GetTable<Customers>()
                                  where c.Name.StartsWith("A")
                                  select c).OrderBy(c => c.City);

var 连接并且不过滤相同项 = (from c in ctx.GetTable<Customers>()
                              where c.City.Contains("A")
                              select c).Concat
                                (from c in ctx.GetTable<Customers>() where c.City.StartsWith("A") select c)
                                .OrderBy(c => c.City);
            //查询城市是 A 打头的顾客和城市包含 A 的顾客的交集,并按照顾客名字排序
            var 取相交项 = (from c in ctx.GetTable<Customers>()
                        where c.City.Contains("A")
                        select c).Intersect
                          (from c in ctx.GetTable<Customers>()
                           where c.City.StartsWith("A")
                           select c).OrderBy(c => c.City);
            //查询城市包含 A 的顾客并从中删除城市以 A 开头的顾客,并按照顾客名字排序
            var 排除相交项 = (from c in ctx.GetTable<Customers>()
                         where c.City.StartsWith("A")
                         select c).Except
                           (from c in ctx.GetTable<Customers>()
                            where c.Name.StartsWith("A")
                            select c).OrderBy(c => c.Name);
            //查询订单数超过 5 的顾客信息
            var 子查询 = from c in ctx.GetTable<Customers>()
                      where
                 (from o in ctx.GetTable<Customers>()
                  group o by o.CustomerID into o
                  where
                      o.Count() > 5
                  select o.Key).Contains(c.CustomerID)
                      select c;

//查询指定城市中的客户
            var in操作 = from c in ctx.GetTable<Customers>()
                       where new string[] { "B", "A", "C" }.Contains(c.City)
                       select c;

//内连接,没有分类的产品查询不到
            var innerjoin = from p in ctx.GetTable<Customers>()
                            join c in ctx.GetTable<Customers>()
                            on p.CustomerID equals c.CustomerID
                            select p.Name;

//外连接,没有分类的产品也能查询到
            var leftjoin = from p in ctx.GetTable<Customers>()
                           join c in ctx.GetTable<Customers>()
                           on p.City equals c.City
                           into pro
                           from x in pro.DefaultIfEmpty()
                           select p.Name;

var 单结果集存储过程 =
                from c in ctx.sp_singleresultset()
                where c.Name.StartsWith("A")
                select c;

var 多结果集存储过程 = ctx.sp_multiresultset();
            var Customer = 多结果集存储过程.GetResult<Customers>();
            var Employees = 多结果集存储过程.GetResult<Employee>();

上一篇:Beta阶段——Scrum 冲刺博客第一天


下一篇:Beta阶段——Scrum 冲刺博客第三天