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

相關文章
相關標籤/搜索