Linq學習總結1--參考Linq技術詳解,1--linq

來源:互聯網
上載者:User

Linq學習總結1--參考Linq技術詳解,1--linq
要點:

1.linq操作的集合必須實現IEnumerable介面,所以在這3.0之前為實現該介面的集合需通過Cast或TypeOf方法轉換成可Linq的集合;


2.查詢式和Lame那啥運算式都可以一起使用.那個方便用哪個,他們只在第一次使用時才會真正去查詢;


List<Employee> ils = new List<Employee>()
         {
             new Employee(){IDCode="jack5",Age=20,littleName="ab"},
             new Employee(){IDCode="mike444",Age=12,littleName="aa"},
             new Employee(){IDCode="mary5",Age=12,littleName="zs"},
             new Employee(){IDCode="sm5555",Age=67,littleName="yb"},
             new Employee(){IDCode="som",Age=67,littleName="cr"}
         };
 
         ArrayList als = new ArrayList()
         {
             new Department(){Name="jack",DepName="富士康"},
             new Department(){Name="jack",DepName="華為"},
             new Department(){Name="mary",DepName="騰訊"},
             new Department(){Name="sum",DepName="移動"},
             new Department(){Name="soom",DepName="聯通"}
         };
 
         #region 查詢語句  第三章,linq技術詳解
         //帶有Into的group by語句
         var va = from c in ils
                  group c by new { c.littleName, c.Age } into g
                  select new { Name = g.Key, ageC = g.Count() };
         var va1 = ils.GroupBy(p => new { p.littleName, p.Age }).Select(p => new { name = p.Key, agec = p.Count() });
 
 
 
         //顯示枚舉變量類型
         var varT = from c in ils
                    join Department d in als on c.IDCode equals d.Name
                    select new { age = c.Age, depName = d.DepName };
         var varT1 = ils.Join(als.Cast<Department>(), c => c.IDCode, p => p.Name, (c, p) => new { age = c.Age, depName = p.DepName });
 
 
         //join語句
         var varJoin = from c in ils
                       join Department d in als
                       on c.IDCode equals d.Name
                       into ao
                       select new { c.IDCode, sum = ao.Sum(p => p.DepName.Length) };
         var varJoin1 = ils.GroupJoin(als.Cast<Department>(), a => a.IDCode, b => b.Name, (b, a) => new { b.IDCode, sum = a.Count() });
 
 
         //Let和Where語句
         var varLet = from c in ils
                      let names = c.IDCode + ":" + c.littleName
                      where names.Length > 5
                      select new { c.Age, names };
         var varLet1 = ils.Select(a => new { a, names = a.IDCode + ":" + a.littleName })
             .Where(p => p.names.Length > 5)
             .Select(b => new { b.a.Age, b.names });
 
 
         //Generator語句(多個 from),orderby語句
         var varSelMany = from a in ils
                          from b in als.Cast<Department>()
                          orderby a.Age, a.Department descending
                          select new { a.IDCode, a.littleName, a.Age, b.DepName };
         var varSelMany1 = ils.SelectMany(p => als.Cast<Department>().Select(a => new { p.Age, a.DepName })).OrderByDescending(a => a.Age).ThenByDescending(a => a.DepName);
 
 
         //group by
         var varGroup = from p in ils
                        group p by p.Age
                            into a
                            select a.Key + ":" + als.Capacity;
          
         #endregion
 
         #region 延遲操作符詳解
         //異常都是ArgumentNullException
         //select,where都有兩個原型,另一個原型有索引參數
         var varWhere = ils.Where((p, i) => i < 2);
 
         //分區操作符 take
         var varTake = ils.Take(2);
 
         //TakeWhile 只要條件不符合就會跳出
         var varTakeWhile = ils.TakeWhile((p, q) => p.IDCode.Length > 4);
 
         //skip 與take互補
         var varSkip = ils.Skip(2);
 
         //skipwhile 與takewhile互補
         var varSkipWhile = ils.SkipWhile((a, i) => a.IDCode.Length > 5 && i < 3);
 
         //串聯操作符
         var varConcat = ils.Take(2).Concat(ils.Skip(2));
         //concat只可以串聯兩個序列,當串聯多個序列的時候可以用SelectMany;
         var varSelectMany1 = new[] { ils.Take(1), ils.Skip(1) }.SelectMany(s => s);
 
         //排序操作,第二個原型可以加參數,比較器,二次排序用thenby,orderbydesding類似
         var varOrderby = ils.OrderBy(p => p.IDCode.Length);
 
         //reverse相反序列輸出
         //Join和JoinGroup p119
         IEnumerable<IGrouping<string,Employee>> items =  ils.GroupBy(p => p.littleName);
         IEnumerable<IGrouping<string,Department>> items1=  ils.GroupBy(p => p.IDCode, q => q.Department);
 
         //集合操作符 distinct,union(並集區別於Concat),intersect(連接後重複元素的序列),except(刪除參數中與自己重複的元素)
         var ca = ils.Distinct();
         List<Employee> ils1 = ils.Take(2).ToList<Employee>();
         ils1.Add(new Employee() { IDCode = "我加的", Age = 33, littleName = "xixi" });
         foreach (var v in ils.Except(ils1))
         {
             Console.WriteLine(v.littleName);
         }
 
         //元素操作符
         var ilsDefaultIfEmpty = ils.Where(p => p.IDCode == "hehe").DefaultIfEmpty().First();
         var ilsDefaultIfEmpty1 = ils.Where(p => p.IDCode == "hehe").DefaultIfEmpty(new Employee() { IDCode="heheid"}).First();
 
         //產生操作符 Enumerable靜態方法Range,Repeat,
         IEnumerable<int> EnumRange =  Enumerable.Range(2, 20);
         foreach (int i in EnumRange)
         {
             Console.WriteLine(i);
         }
         //p145
          
         string str = string.Empty;
         //cast,ofType,AsEnumerable()[將序列編程式列,適用於Linq To Sql]
          
 
         #endregion

本文章知識由 擬人句 www.zaojuzi.com 整理髮布


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.