LINQ 函数的实战演练测试
1、首先定义一个图书类。专门存放图书的属性信息。
代码如下:
1 //Book.cs 2 3 using System; 4 5 namespace LinqTest 6 { 7 public class Book 8 { 9 public string Name { get; set; } 10 public string Author { get; set; } 11 public decimal Price { get; set; } 12 public DateTime ReleaseDate { get; set; } 13 14 public Book() 15 { 16 17 } 18 19 public Book(string name, string anthor, decimal price, DateTime releasedata) 20 { 21 Name = name; 22 Author = anthor; 23 Price = price; 24 ReleaseDate = releasedata; 25 } 26 } 27 } 28
2、接着新建一个类专门用来对图书类进行操作。代码如下:
1 //BookManage.cs 2 3 using System; 4 using System.Collections.Generic; 5 6 namespace LinqTest 7 { 8 public static class BookManage 9 { 10 private static List<Book> _books; 11 12 public static IList<Book> GetBook() 13 { 14 return _books ?? (_books = new List<Book>() 15 { 16 new Book("He", "Joke", 133, new DateTime(20160101)), 17 new Book("She", "Bob", 222, new DateTime(20110603)), 18 new Book("I", "Cham", 999, new DateTime(19910602)) 19 }); 20 } 21 } 22 }
3、最后在主程序中执行。
1 //Program.cs 2 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 7 namespace LinqTest 8 { 9 internal class Program 10 { 11 private static void Main() 12 { 13 Console.WriteLine("/**Select Test*/\n"); 14 var test = BookManage.GetBook().Where(c => c.Author.StartsWith("J")).Select(c => new 15 { 16 c.Name, 17 c.Author, 18 c.Price, 19 c.ReleaseDate 20 }); 21 foreach (var i in test) 22 { 23 Console.WriteLine(i); 24 } 25 26 Console.WriteLine("\n/**SelectMany Test*/"); 27 var list1 = new List<int>() { 1, 2, 3, 4, 5, 6 }; 28 var list2 = new List<int>() { 3, 2, 3, 1 }; 29 30 var query = list1.SelectMany(c => list2); 31 foreach (var variable in query) 32 { 33 Console.Write("{0}", variable); 34 } 35 Console.WriteLine("\n"); 36 37 Console.WriteLine("\n/*GroupBy test**/\n"); 38 var list3 = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 39 40 var group = list3.GroupBy(c => c); 41 42 foreach (var variable in group) 43 { 44 Console.WriteLine("{0} Count : {1}", variable.Key, variable.Count()); 45 } 46 Console.WriteLine("\n/*ToLookUp**/\n"); 47 var list4 = new List<Book>() 48 { 49 new Book("A","B",11,new DateTime(123146)), 50 new Book("AA","BB",222,new DateTime(4567895)), 51 new Book("AAA","BBB",333,new DateTime(4564654)), 52 new Book("A","BBB",444,new DateTime(7898465)) 53 }; 54 55 var lookup = list4.ToLookup(c => c.Name); 56 57 foreach (var v in lookup) 58 { 59 Console.WriteLine("=====Group : {0}====", v.Key); 60 foreach (var g in v) 61 { 62 Console.WriteLine("Author: {0} ,Price: {1}, Release: {2}", g.Author, g.Price, g.ReleaseDate); 63 } 64 } 65 66 Console.WriteLine("\nOderBy And Thenby Test!\n"); 67 var orderquery = list4.OrderBy(c => c.Price).ThenBy(c => c.Name).Select(c => new 68 { 69 c.Name, 70 c.Author, 71 c.Price, 72 c.ReleaseDate 73 }); 74 foreach (var item in orderquery) 75 { 76 Console.WriteLine(item); 77 } 78 79 Console.WriteLine("\n获取集合测试!\n"); 80 var list5 = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 81 82 var newList = list5.Where(c => c > 3).ToList().Select(c => new 83 { 84 c 85 }); 86 87 foreach (var r in newList) 88 { 89 Console.WriteLine(r); 90 } 91 92 Console.WriteLine(" \n聚合和汇总测试\n"); 93 const double myBalance = 100.0; 94 int[] withdramItem = { 20, 30, 40, 50, 60, 70, 80 }; 95 var banlance = withdramItem.Aggregate(myBalance, (originbalance, nextwithdraw) => 96 { 97 Console.WriteLine("originbalance:{0},nextwithdraw {1}", originbalance, nextwithdraw); 98 Console.WriteLine("Withdraw status:{0}", (nextwithdraw <= originbalance) ? "OK" : "Failed"); 99 return ((nextwithdraw <= originbalance) ? (originbalance - nextwithdraw) : originbalance); 100 }); 101 Console.WriteLine("Ending Balance:{0}", banlance); 102 103 Console.WriteLine("\n***筛选***\n"); 104 105 var query1 = BookManage.GetBook().Where(c => c.Price > 1).Select(c => new 106 { 107 c.Name, 108 c.Author, 109 c.Price, 110 c.ReleaseDate 111 }).Take(2); 112 foreach (var r in query1) 113 { 114 Console.WriteLine("{0}", r); 115 } 116 } 117 } 118 }
4、运行结果如下: