Ling && Lambda

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace Test_Lambda_Linq
  8 {
  9     class Program
 10     {
 11         static List<Person> PersonList = new List<Person>()
 12             {
 13                 new Person(){ Id=1, Age=10, Name="James", Gender="Male"},
 14                 new Person(){ Id=2, Age=20, Name="Harden", Gender="Male" },
 15                 new Person(){ Id=3, Age=30, Name="George", Gender="Female"},
 16                 new Person(){ Id=4, Age=40, Name="Ball", Gender="Female"},
 17                 new Person(){ Id=5, Age=50, Name="Kobe", Gender="Male"},
 18                 new Person(){ Id=6, Age=60, Name="Fames", Gender="Male"},
 19                 new Person(){ Id=7, Age=70, Name="Garden", Gender="Male" },
 20                 new Person(){ Id=8, Age=80, Name="Deorge", Gender="Female"},
 21                 new Person(){ Id=9, Age=90, Name="Hall", Gender="Female"},
 22                 new Person(){ Id=10, Age=100, Name="Dobe", Gender="Male"},
 23                 new Person(){ Id=11, Age=110, Name="Lames", Gender="Male"},
 24                 new Person(){ Id=12, Age=120, Name="Parden", Gender="Male" },
 25                 new Person(){ Id=13, Age=130, Name="Keorge", Gender="Female"},
 26                 new Person(){ Id=14, Age=140, Name="Yall", Gender="Female"},
 27                 new Person(){ Id=15, Age=150, Name="Robe", Gender="Male"}
 28             };
 29         static List<Children> ChildList = new List<Children>()
 30         {
 31             new Children(){ Id=1, Name="Tomas", Age=5, Class=2, Gender="Female"},
 32             new Children(){ Id=2, Name="James", Age=10, Class=1, Gender="Male"},
 33             new Children(){ Id=3, Name="Jungle", Age=12, Class=3, Gender="Male"}
 34         };
 35         static void Main(string[] args)
 36         {
 37             //PersonList.Reverse();//全盘逆序
 38             //PersonList.Reverse(0,4);//指定范围逆序
 39             #region Lambda
 40             #region Sort排序
 41             //ServiceInvoke.Invoke(() =>
 42             //{
 43             //    foreach (var p in PersonList)
 44             //    {
 45             //        Console.WriteLine(p.Name);
 46             //    }
 47             //});
 48 
 49             //PersonList.Sort((p1, p2) => { return p1.Age - p2.Age; });
 50             //PersonList.Sort((p1, p2) =>  p1.Age - p2.Age );//排序
 51 
 52             //ServiceInvoke.Invoke(() =>
 53             //{
 54             //    foreach (var p in PersonList)
 55             //    {
 56             //        Console.WriteLine(p.Name);
 57             //    }
 58             //});
 59             #endregion
 60 
 61             #region Where筛选高手
 62             //ServiceInvoke.Invoke(() =>
 63             //{
 64             //    List<Person> pTemp = PersonList.Where(p => p.Age > 20 && p.Gender == "Male").ToList();
 65             //    foreach (var p in pTemp)
 66             //    {
 67             //        Console.WriteLine(p.Name + " is " + p.Age + " years old ");
 68             //    };
 69             //    PersonList.ForEach(p => Console.WriteLine(p.Name));
 70             //});
 71             #endregion
 72 
 73             #region Select投影大牛---查询投影
 74             //ServiceInvoke.Invoke(() => 
 75             //{
 76             //    List<Person> pTemp = PersonList.Where(p => p.Gender == "Male").Select(p => (Person)new LitePerson() { Name = p.Name , Age=p.Age}).ToList();
 77             //    foreach (var p in pTemp)
 78             //    {
 79             //        Console.WriteLine(p.Name + " is " + p.Age + " years old ");
 80             //    };
 81             //    PersonList.ForEach(p => Console.WriteLine(p.Name));
 82             //});
 83             #endregion
 84 
 85             #region OrderBy排序 第二次用ThenBy排序
 86             //ServiceInvoke.Invoke(() =>
 87             //{
 88             //    Console.WriteLine("Initial Order:"); PersonList.ForEach(p => Console.WriteLine(p.Name + " is " + p.Age + " years old "));//原来
 89             //    List<Person> pTempDefault = PersonList.OrderBy(p => p.Age).ToList(); Console.WriteLine("Default Order:");//默认由小到大---升序
 90             //    pTempDefault.ForEach(p => Console.WriteLine(p.Name + " is " + p.Age + " years old "));//排序后
 91 
 92             //    //单条件降序
 93             //    List<Person> pTempDescending = PersonList.OrderByDescending(p => p.Age).ToList(); Console.WriteLine("Descending Order:");//降序
 94             //    pTempDescending.ForEach(p => Console.WriteLine(p.Name + " is " + p.Age + " years old "));
 95 
 96             //    //多条件综合排序
 97             //    List<Person> pTempOrderBy_ThenByDescending = PersonList.OrderBy(p => p.Age).ThenByDescending(p => p.Id).ToList(); Console.WriteLine("OrderBy_ThenByDescending Order:");
 98             //    pTempOrderBy_ThenByDescending.ForEach(p => Console.WriteLine(p.Name + " is " + p.Age + " years old " + " And his Id is " + p.Id));
 99             //});
100             #endregion
101 
102             #region Join 多个数据集(>=2)   
103             //ServiceInvoke.Invoke(() =>
104             //{
105             //    var joinedList = PersonList.Join(ChildList, p => p.Id, c => c.Id, (p, c) =>
106             //    new { //匿名对象
107             //        ParentID = p.Id,
108             //        ChildID = c.Id,
109             //        ParentName = p.Name,
110             //        ChildName = c.Name
111             //    }).ToList();
112             //    joinedList.ForEach(j => Console.WriteLine(" ParentID: " + j.ParentID + " ChildID: " + j.ChildID + " ParentName: " + j.ParentName + " ChildName: " + j.ChildName));
113             //});
114             #endregion
115 
116             #region GroupBy
117             //ServiceInvoke.Invoke(() =>
118             //{
119             //    IEnumerable<IGrouping<string, Person>> groups = PersonList.GroupBy(p => p.Gender);
120             //    IList<IGrouping<string, Person>> groupList = groups.ToList();
121             //    foreach (var g in groupList)
122             //    {
123             //        //Key是分组依据的类型---Gender
124             //        Console.WriteLine("Group:{0}", g.Key=="Male" ? "Male" : "Female");
125             //        foreach (Person p in g)
126             //        {
127             //            Console.WriteLine(p.Name + " is " + p.Gender);
128             //        }
129             //    }
130             //});
131             #endregion
132 
133             #region Skip与Take
134             //实现分页
135             //ServiceInvoke.Invoke(()=> 
136             //{
137             //    Console.WriteLine("First Page:");
138             //    var firstPageData = GetPagedListByIndex(1,5);
139             //    firstPageData.ForEach(p => Console.WriteLine(p.Name + " is " + p.Age + " years old " + " And his Id is " + p.Id));
140             //    Console.WriteLine("Second Page:");
141             //    var secondPageData = GetPagedListByIndex(2, 5);
142             //    secondPageData.ForEach(p => Console.WriteLine(p.Name + " is " + p.Age + " years old " + " And his Id is " + p.Id));
143             //    Console.WriteLine("Third Page:");
144             //    var thirdPageData = GetPagedListByIndex(1, 5);
145             //    thirdPageData.ForEach(p => Console.WriteLine(p.Name + " is " + p.Age + " years old " + " And his Id is " + p.Id));
146             //});
147             #endregion
148             #endregion
149 
150             #region Linq
151 
152             #region 基本查询
153             //ServiceInvoke.Invoke(()=> 
154             //{
155             //    var maleList = from p in PersonList
156             //                   where p.Gender == "Male"
157             //                   select p;
158             //    maleList.ToList().ForEach(p => Console.WriteLine(p.Name + " is " + p.Gender));
159             //});
160             #endregion
161 
162             #region 排序条件查询
163             //ServiceInvoke.Invoke(() =>
164             //{
165             //    var orderList = from p in PersonList
166             //                   orderby p.Age descending//先按年龄排序
167             //                   orderby p.Name ascending//在上面的基础上按姓名排序
168             //                   select p;
169             //    orderList.ToList().ForEach(p => Console.WriteLine(p.Name + " is " + p.Age));
170             //});
171             #endregion
172 
173             #region 连接查询
174             //ServiceInvoke.Invoke(() =>
175             //{
176             //    var joinedList = from p in PersonList
177             //                     join c in ChildList
178             //                     on p.Id equals c.Id
179             //                     select new
180             //                     {
181             //                         Person = p,
182             //                         Child = c
183             //                     };
184             //    joinedList.ToList().ForEach(p => Console.WriteLine(" Person.Name is " + p.Person.Name + " Child.Name is " + p.Child.Name + " And Id is: "+ p.Child.Id));
185             //});
186             #endregion
187 
188             #region 分组查询
189             ServiceInvoke.Invoke(() =>
190             {
191                 var groupList = from p in PersonList
192                                  group p by p.Gender;
193                 foreach (var g in groupList)
194                 {
195                     Console.WriteLine($"Group:{g.Key}");
196                     foreach (var item in g)
197                     {
198                         Console.WriteLine(item.Name + " is "+ item.Gender);
199                     }
200                 }
201             });
202             #endregion
203 
204             #endregion
205         }
206 
207         static List<Person> GetPagedListByIndex(int pageIndex, int pageSize)
208         {
209             List<Person> dataList = PersonList;
210             return dataList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
211         }
212     }
213 }
原文地址:https://www.cnblogs.com/anwser-jungle/p/8418606.html