Lambda表达式与标准运算符查询
class Program { static void Main(string[] args) { //Lambda表达式输出List集合每一项 List<string> list = new List<string>() { "aaa", "bbb", "ccc", "ddd", "eee" }; list.ForEach(d => Console.WriteLine(d)); //Lambda表达式查找匹配元素 List<A1> a1 = new List<A1>(); a1.Add(new A1 { Id = 10, Name = "aaa" }); a1.Add(new A1 { Id = 11, Name = "bbb" }); a1.Add(new A1 { Id = 12, Name = "ccc" }); a1.Add(new A1 { Id = 13, Name = "ddd" }); List<A1> list1 = a1.FindAll(d => d.Id > 11); //Lambda排序 a1.Sort((x, y) => y.Id - x.Id); //Lambda匹配指定元素到新集合 var s1 = a1.Select(d => new A2() { Name = d.Name }); foreach (var item in s1) { Console.WriteLine(item.Name); } //Lambda演变过程 var gg = new Func<string, int>(delegate(string str) { return str.Length; }); int s = gg("abcde"); var ss = delegate(string str) { return str.Length; }; var qq = (string str) => { return str.Length; }; var ww = (string str) => str.Length; var ee = str => str.Length; var bb = new Func<int, int, int>(delegate(int x, int y) { return x + y; }); int cc = bb(5,10); Console.ReadKey(); } } public class A1 { public int Id { get; set; } public string Name { get; set; } } public class A2 { public string Name { get; set; } }
标准查询运算符-Where-Select-Order-Join
List<A1> a1 = new List<A1>(); a1.Add(new A1 { Id = 10, Name = "aaa", Age = 101, Gender = true }); a1.Add(new A1 { Id = 13, Name = "bbb", Age = 21, Gender = true }); a1.Add(new A1 { Id = 12, Name = "ccc", Age = 21, Gender = false }); a1.Add(new A1 { Id = 13, Name = "ddd", Age = 200, Gender = true }); List<DogToy> a2 = new List<DogToy>(); a2.Add(new DogToy { DogId = 11, DogName = "小白" }); a2.Add(new DogToy { DogId = 10, DogName = "小欢" }); a2.Add(new DogToy { DogId = 12, DogName = "小美" }); a2.Add(new DogToy { DogId = 15, DogName = "小王" }); //Join 连接集合 //返回的结果是一个匿名类集合,程序员可以手动的控制返回集合里使用联合查询 //两个集合里的的哪些属性 var listJoined = a1.Join(a2, d => d.Id, t => t.DogId, (d, t) => new { DogId = d.Id, DogName = d.Name, ToyName = t.DogName }).ToList(); listJoined.ForEach(d=>Console.WriteLine(d.ToString())); //a1.ForEach(d => { Console.WriteLine(d.ToString()); }); //Where查询 //var aa = a1.Where(d => d.Gender == false); //链式编程 //var aa = a1.Where(d => d.Gender == false).Select(d => new A2() { Name = d.Name }); //升序排序 //var aa = a1.OrderBy(d => d.Age).ToList(); //升序后再按指定列排序 var aa = a1.OrderBy(d => d.Age).ThenBy(d => d.Id).ToList(); //降序排序 //var aa = a1.OrderByDescending(d => d.Age).ToList(); //aa.ForEach(d => Console.WriteLine(d.ToString()));
标准查询运算符-GroupBy分组
List<A1> a1 = new List<A1>(); a1.Add(new A1 { Id = 10, Name = "aaa", Age = 101, Gender = true }); a1.Add(new A1 { Id = 13, Name = "bbb", Age = 21, Gender = true }); a1.Add(new A1 { Id = 12, Name = "ccc", Age = 21, Gender = false }); a1.Add(new A1 { Id = 13, Name = "ddd", Age = 200, Gender = false }); List<DogToy> a2 = new List<DogToy>(); a2.Add(new DogToy { DogId = 11, DogName = "小白" }); a2.Add(new DogToy { DogId = 10, DogName = "小欢" }); a2.Add(new DogToy { DogId = 12, DogName = "小美" }); a2.Add(new DogToy { DogId = 15, DogName = "小王" }); //Join 连接集合 //返回的结果是一个匿名类集合,程序员可以手动的控制返回集合里使用联合查询 //两个集合里的的哪些属性 var listJoined = a1.Join(a2, d => d.Id, t => t.DogId, (d, t) => new { DogId = d.Id, DogName = d.Name, ToyName = t.DogName }).ToList(); //遍历输出 listJoined.ForEach(d=>Console.WriteLine(d.ToString())); //GroupBy分组,将姓别分组 var listGroup = a1.GroupBy(d => d.Gender); foreach (var group in listGroup) { //输出小组的分组条件 Console.WriteLine("小组:" + group.Key); //遍历小组里所有的元素 foreach (var item in group) { Console.WriteLine(item.ToString()); } } //a1.ForEach(d => { Console.WriteLine(d.ToString()); }); //Where查询 var aa = a1.Where(d => d.Gender == false); //链式编程 var aa = a1.Where(d => d.Gender == false).Select(d => new A2() { Name = d.Name }); //升序排序 var aa = a1.OrderBy(d => d.Age).ToList(); //升序后再按指定列排序 var aa = a1.OrderBy(d => d.Age).ThenBy(d => d.Id).ToList(); //降序排序 var aa = a1.OrderByDescending(d => d.Age).ToList(); aa.ForEach(d => Console.WriteLine(d.ToString()));
//分页
List<A1> a1 = new List<A1>(); a1.Add(new A1 { Id = 10, Name = "aaa", Age = 101, Gender = true }); a1.Add(new A1 { Id = 15, Name = "bbb", Age = 21, Gender = true }); a1.Add(new A1 { Id = 12, Name = "ccc", Age = 21, Gender = false }); a1.Add(new A1 { Id = 13, Name = "ddd", Age = 200, Gender = false }); var list = a1.OrderBy(d => d.Id).ToList(); //分页,Skip(0)跳过0行,Take(2)分两行 //var pageOne = list.Skip(0).Take(2); var pageOne = list.Skip(0).Take(10).ToList(); Console.WriteLine("第一页"); pageOne.ForEach(i => Console.WriteLine(i.ToString())); var pageTwo = list.Skip(10).Take(10).ToList(); Console.WriteLine("第二页"); pageTwo.ForEach(i => Console.WriteLine(i.ToString())); var pageOne1 = GetPagedListByIndex(a1, 1); Console.WriteLine("第一页"); pageOne1.ForEach(i => Console.WriteLine(i.ToString())); var pageTwo2 = GetPagedListByIndex(a1, 2); Console.WriteLine("第二页"); pageTwo2.ForEach(i => Console.WriteLine(i.ToString())); Console.ReadKey(); } //分页方法 static List<A1> GetPagedListByIndex(List<A1> groupList, int pageIndex) { int pageSize = 10; return groupList.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); }