using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace LibraryLearn1 { public class Class1 { static List<Dog> list_dog = new List<Dog>() { new Dog{ID=1,Name="小黑",Age=3,Gender=true}, new Dog{ID=2,Name="小花",Age=2,Gender=false}, new Dog{ID=3,Name="小白",Age=3,Gender=true}, new Dog{ID=4,Name="小黄",Age=6,Gender=false}, new Dog{ID=5,Name="小蓝",Age=5,Gender=true}, new Dog{ID=6,Name="小灰",Age=8,Gender=false} }; static List<DogToy> list_dogtoy = new List<DogToy>() { new DogToy{ID=1,Name="小黑",ID_Dog=1}, new DogToy{ID=2,Name="小花",ID_Dog=2}, new DogToy{ID=1,Name="小白",ID_Dog=2}, new DogToy{ID=2,Name="小黄",ID_Dog=3}, new DogToy{ID=1,Name="小蓝",ID_Dog=3}, new DogToy{ID=2,Name="小灰",ID_Dog=2} }; //筛选集合Where需要提供一个带bool返回值的“筛选器”,从而表明集合中某个元素是否应该被返回。 public void whereLambda() { List<Dog> list_result = list_dog.Where(c => c.Gender == false && c.Age > 4).ToList(); } //查询投射,返回新对象集合 IEnumerable<TSource> Select() public void selectLambda() { List<SmallDog> list = list_dog.Where(c => c.Gender == false && c.Age > 4).Select(c => new SmallDog { Name = c.Name }).ToList(); } //多条件排序 OrderBy().ThenBy().ThenBy() public void orderLambda() { List<Dog> list_order = list_dog.OrderBy(c => c.ID).ThenByDescending(c => c.Age).ToList(); } //集合连接 Join() public void joinLambda() { var list_join = list_dog.Join(list_dogtoy, d => d.ID, dt => dt.ID_Dog, (d, dt) => new { DogName = d.Name, DogToyName = dt.Name }).ToList(); list_join.ForEach(c => Console.Write(c.ToString()));//匿名类tostring()把所有的对象相加 } //gropu by 分组 public void groupbyLamdbda() { var list_group= list_dog.GroupBy(c => c.Gender).ToList(); } //Page分页 public void PageLambda() {//假设一页两行 int pagesize = 2; //第一页 int pageIndex1 = 1; var list_page1 = list_dog.Skip((pageIndex1-1)*pagesize).Take(pagesize).ToList(); //第二页 int pageIndex2 = 2; var list_page2 = list_dog.Skip((pageIndex2 - 1) * pagesize).Take(pagesize).ToList(); } } public class Dog { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } public bool Gender { get; set; } } public class SmallDog { public string Name { get; set; } } public class DogToy { public int ID { get; set; } public int ID_Dog { get; set; } public string Name { get; set; } } }