1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Linq 8 { 9 class LinqToObject 10 { 11 private List<Student> listStu = null; 12 private List<ClassStu> listClass = null; 13 private Random random = new Random(); 14 public void SetList() 15 { 16 17 listStu = new List<Student>(); 18 listClass = new List<ClassStu>(); 19 for (int i = 0; i < 30; i++) 20 { 21 var student = new Student() 22 { 23 id=random.Next(1,30), 24 name=$"阿毛{random.Next(0,30)}", 25 age = random.Next(10, 40) 26 }; 27 var classStu = new ClassStu() 28 { 29 id = random.Next(1, 30), 30 name = $"班级{random.Next(1, 20)}", 31 number = random.Next(20, 50) 32 }; 33 34 listStu.Add(student); 35 listClass.Add(classStu); 36 } 37 } 38 39 public void Show() 40 { 41 SetList(); 42 listStu.Where<Student>(s => s.age < 10).ToList().ForEach((m) => Console.WriteLine($"该学生的年龄小于10的有{m.name}==>{m.age}")); 43 } 44 45 //延迟加载 46 public void DelayShow() 47 { 48 SetList(); 49 { 50 Console.WriteLine("**************************************"); 51 //查询运算符 52 var list = Enumerable.Where(listStu, (s) => 53 { 54 Console.WriteLine("123"); //在返回结果之前添加一些别的可以延迟 55 return s.age < 10; 56 }); 57 foreach (var item in list) 58 { 59 Console.WriteLine($"该学生的年龄小于10的有{item.name}==>{item.age}"); 60 } 61 } 62 63 { 64 //投影第1种 65 Console.WriteLine("**************************************"); 66 //查询运算符 67 var list = Enumerable.Where(listStu, (s) => 68 { 69 Console.WriteLine("123"); 70 return s.age < 10; 71 }).Select(s => new 72 { 73 newId = s.id + "**", 74 newName = s.name + "###", 75 newAge = s.age + "++++" 76 }) 77 .OrderBy(ns => ns.newId) //可做排序,正序 78 79 .OrderByDescending(ns => ns.newAge) //倒序 80 81 .ThenBy(ns=>ns.newAge) //再次排序 82 83 .Skip(10) //可做分页,跳过多少条数据 84 85 .Take(3); //获取多少条数据 86 87 foreach (var item in list) 88 { 89 Console.WriteLine($"该学生的年龄小于10的有{item.newId}==>{item.newName}===>{item.newAge}"); 90 } 91 } 92 93 { 94 //内连接 95 Console.WriteLine("**************************************"); 96 var list = Enumerable.Join(listStu, listClass, m => m.id, n => n.id, (m, n) => new 97 { 98 stuId=m.id, 99 classId=n.id 100 }).DefaultIfEmpty(); // 如果要写左连接,则直接在末尾加上DefaultIfEmpty(),去掉则为内连接 101 102 foreach (var item in list) 103 { 104 Console.WriteLine($"内连接:stuId={item.stuId},classId={item.classId}"); 105 } 106 } 107 108 109 Console.WriteLine("##############################################################################"); 110 111 { 112 //查询表达式 113 Console.WriteLine("**************************************"); 114 var list = from s in listStu 115 where s.age < 10 116 select s; 117 foreach (var item in list) 118 { 119 Console.WriteLine($"该学生的年龄小于10的有{item.name}==>{item.age}"); 120 } 121 } 122 123 { 124 //投影第2种 125 Console.WriteLine("**************************************"); 126 var list = from s in listStu 127 where s.age < 10 128 orderby s.age descending 129 select new //挑选出学生年龄小于10的学生后,再创建 新的对象 进行投影,new{}为匿名对象 130 { 131 newId = s.id + "**", 132 newName=s.name+"###", 133 newAge=s.age+"++++" 134 }; 135 136 //list为新对象集合 137 foreach (var item in list) 138 { 139 Console.WriteLine($"该学生的年龄小于10的有{item.newId}==>{item.newName}===>{item.newAge}"); 140 } 141 } 142 143 { 144 //内连接第二种 145 Console.WriteLine("**************************************"); 146 var list = from s in listStu 147 join c in listClass on s.id equals c.id 148 select new //挑选出学生年龄小于10的学生后,再创建 新的对象 进行投影,new{}为匿名对象 149 { 150 newId = s.id +c.id, 151 newName = s.name + c.name, 152 newAge = s.age + c.number 153 }; 154 155 //list为新对象集合 156 foreach (var item in list) 157 { 158 Console.WriteLine($"该学生的年龄小于10的有{item.newId}==>{item.newName}===>{item.newAge}"); 159 } 160 } 161 162 { 163 //左连接 164 Console.WriteLine("**************************************"); 165 var list = from s in listStu 166 join c in listClass on s.id equals c.id 167 into scList //将连接放进一个全新的集合中 168 from sc in scList.DefaultIfEmpty() 169 select new 170 { 171 Name = s.name, 172 ClassName=sc==null?"无班级":sc.name //c变sc,为空则用 173 }; 174 175 //list为新对象集合 176 foreach (var item in list) 177 { 178 Console.WriteLine($"该学生的年龄小于10的有{item.Name}==>{item.ClassName}"); 179 } 180 } 181 } 182 } 183 }
下面是一些Model类
1 class Student 2 { 3 public int id { get; set; } 4 public string name { get; set; } 5 public int age { get; set; } 6 } 7 8 class ClassStu 9 { 10 public int id { get; set; } 11 public string name { get; set; } 12 public int number { get; set; } 13 }