首页 > 其他 > 详细

Linq学习总结1--参考Linq技术详解

时间:2015-03-14 23:09:41      阅读:352      评论:0      收藏:0      [点我收藏+]

2个要点:

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

  

Linq学习总结1--参考Linq技术详解

原文:http://www.cnblogs.com/fanglorry/p/4338481.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!