码迷,mamicode.com
首页 > 其他好文 > 详细

Linq的基础2

时间:2015-11-10 23:47:12      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:

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>();

Linq的基础2

标签:

原文地址:http://www.cnblogs.com/qianlovebeijixiong/p/4954936.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!