***

6585 ワード

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