1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Data.Linq.SqlClient; 7 8 namespace _15Linq 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 15 //int a = 10; 16 //var y = a < 10 ? a = 0 : ++a; 17 18 19 20 21 //Console.WriteLine(y); 22 23 //Console.Read(); 24 25 //新增学生 26 List<Student> studentList = new List<Student> 27 { 28 new Student {Sno="108",Sname="曾华",Ssex="男",Sbirthday=DateTime.Parse("1977/09/01"),Class="95033" }, 29 new Student {Sno="105",Sname="匡明",Ssex="男",Sbirthday=DateTime.Parse("1916/10/2"),Class="95031" }, 30 new Student {Sno="107",Sname="王丽",Ssex="女",Sbirthday=DateTime.Parse("1976/1/23"),Class="95033" }, 31 new Student {Sno="101",Sname="李军",Ssex="男",Sbirthday=DateTime.Parse("1976/2/20"),Class="95033" }, 32 new Student {Sno="109",Sname="王芳",Ssex="女",Sbirthday=DateTime.Parse("1975/2/10"),Class="95031" }, 33 new Student {Sno="103",Sname="陆君",Ssex="男",Sbirthday=DateTime.Parse("1974/6/3"),Class="95031" } 34 }; 35 36 //新增教师 37 List<Teacher> teacherList = new List<Teacher> 38 { 39 new Teacher {Tno="804",Tname="李诚",Tsex="男",Tbirthday=DateTime.Parse("1958-12-02"),Prof="副教授",Depart="计算机系" }, 40 new Teacher {Tno="856",Tname="张旭",Tsex="男",Tbirthday=DateTime.Parse("1969-03-12"),Prof="讲师",Depart="电子工程系" }, 41 new Teacher {Tno="825",Tname="王萍",Tsex="女",Tbirthday=DateTime.Parse("1972-05-05"),Prof="助教",Depart="计算机系" }, 42 new Teacher {Tno="831",Tname="刘冰",Tsex="女",Tbirthday=DateTime.Parse("1977-08-14"),Prof="助教",Depart="电子工程系" }, 43 }; 44 45 //新增课程 46 List<Course> courseList = new List<Course> 47 { 48 new Course {Cno="3-105",Cname="计算机导论",Tno="825" }, 49 new Course {Cno="3-245",Cname="操作系统",Tno="804" }, 50 new Course {Cno="6-166",Cname="数字电路",Tno="856" }, 51 new Course {Cno="9-888",Cname="高等数学",Tno="831" }, 52 }; 53 54 //新增成绩 55 List<Score> scoreList = new List<Score> 56 { 57 new Score {Sno="103",Cno="3-245",Degree=86 }, 58 new Score {Sno="105",Cno="3-245",Degree=75 }, 59 new Score {Sno="109",Cno="3-245",Degree=68 }, 60 new Score {Sno="103",Cno="3-105",Degree=92 }, 61 new Score {Sno="105",Cno="3-105",Degree=88 }, 62 new Score {Sno="109",Cno="3-105",Degree=76 }, 63 new Score {Sno="101",Cno="3-105",Degree=64 }, 64 new Score {Sno="107",Cno="3-105",Degree=91 }, 65 new Score {Sno="108",Cno="3-105",Degree=78 }, //变换 66 new Score {Sno="101",Cno="6-166",Degree=85 }, 67 new Score {Sno="107",Cno="6-166",Degree=79 }, 68 new Score {Sno="108",Cno="6-166",Degree=81 }, 69 //new Score {Sno="108",Cno="3-105",Degree=92 } 70 }; 71 72 Console.WriteLine("1.--------【查询全部列】查询Student表中的所有记录 --------"); 73 var student = from s in studentList 74 select s; 75 Console.WriteLine(student); 76 var query1 = from s in studentList 77 select s; 78 foreach(var s in query1) 79 { 80 Console.WriteLine("Sno:{0},Sname:{1},Ssex:{2},Class:{3}", s.Sno, s.Sname, s.Ssex, s.Class); 81 } 82 83 84 //匿名类型是C#3.0中新特性。其实质是编译器根据我们自定义自动产生一个匿名的类来帮助我们实现临时变量的储存。 85 //匿名类型还依赖于另外一个特性:支持根据property来创建对象 86 Console.WriteLine(""); 87 Console.WriteLine("2.--------【查询指定列】询Student表中的所有记录的Sname、Ssex和Class列--------"); 88 89 var query2 = from u in studentList 90 select new { u.Sname, u.Ssex, u.Class }; 91 foreach (var s in query2) 92 { 93 Console.WriteLine("Sname:{0},Ssex:{1},Class:{2}",s.Sname, s.Ssex, s.Class); 94 } 95 96 97 //SqlMethods.DateDiffYear函数是属于命名空间System.Data.Linq.SqlClient,需要引用类库:System.Data.Linq; 98 Console.WriteLine(""); 99 Console.WriteLine("3.--------【日期函数】查询Student表中的所有记录的Sname、Ssex和Class列和年龄(通过日期函数计算得到)--------"); 100 var query3 = from s in studentList 101 select new { s.Sname, s.Ssex, s.Class, Age = SqlMethods.DateDiffYear(s.Sbirthday, DateTime.Now) }; 102 103 foreach(var s in query3) 104 { 105 Console.WriteLine("Sname:{0},Ssex:{1},Class:{2} Age:{3}", s.Sname, s.Ssex, s.Class,s.Age); 106 } 107 108 109 Console.WriteLine(""); 110 Console.WriteLine("4.--------【匿名类型中引入表达式】查询Student表中的所有记录的Sname、Ssex和Class列和年龄(通过日期函数计算得到),寿命状态(<80:短命,>=80:高寿)--------"); 111 var query4 = from s in studentList 112 select new { s.Sname, s.Ssex, s.Class, Age = SqlMethods.DateDiffYear(s.Sbirthday, DateTime.Now), lifetime = SqlMethods.DateDiffYear(s.Sbirthday, DateTime.Now) >= 80 ? "高寿" : "短命" }; 113 114 foreach (var s in query4) 115 { 116 Console.WriteLine("Sname:{0},Ssex:{1},Class:{2} Age:{3} lifetime:{4}", s.Sname, s.Ssex, s.Class, s.Age,s.lifetime); 117 } 118 119 Console.WriteLine(""); 120 Console.WriteLine("5.--------【去掉重复项】查询教师所有的单位即不重复的Depart列--------"); 121 122 var query5 = (from s in teacherList 123 select s.Depart).Distinct(); 124 125 foreach(var s in query5) 126 { 127 Console.WriteLine("Depart:{0}", s); 128 } 129 130 131 Console.WriteLine(""); 132 Console.WriteLine("6.--------【去掉重复项】查询教师所有的单位和职称(都相同的去掉)--------"); 133 var query6 = (from s in teacherList 134 select new { s.Depart, s.Prof }).Distinct(); 135 foreach (var s in query6) 136 { 137 Console.WriteLine("Depart:{0} Prof:{1}", s.Depart,s.Prof); 138 } 139 140 Console.WriteLine(""); 141 Console.WriteLine("7.--------【条件查询-关系运算与逻辑运算】查询Score表中成绩在80到100之间的所有记录--------"); 142 var query7 = from s in scoreList 143 where s.Degree <= 100 && s.Degree >= 80 144 select s; 145 foreach(var s in query7) 146 { 147 Console.WriteLine("Sno:{0},Cno:{1},Degree:{2}", s.Sno, s.Cno, s.Degree); 148 } 149 150 151 Console.WriteLine(""); 152 Console.WriteLine("8.-------【模糊查询,字符串包含,以及字符串比较等于查询】 查询学生姓名包含王的学生信息"); 153 var query8 = from s in studentList 154 where s.Sname.Contains("王") 155 select s; 156 foreach (var s in query8) 157 { 158 Console.WriteLine("Sno:{0},Sname:{1},Ssex:{2},Class:{3}", s.Sno, s.Sname, s.Ssex, s.Class); 159 } 160 161 162 Console.WriteLine(""); 163 Console.WriteLine("9.--------【条件查询-in查询】查询Score表中成绩为85,86或88的记录。--------"); 164 decimal[] arr = { 85,86,88}; 165 166 var query9 = from s in scoreList 167 where new decimal[] { 85, 86, 88 }.Contains(s.Degree) 168 select s; 169 foreach (var s in query9) 170 { 171 Console.WriteLine("Sno:{0},Cno:{1},Degree:{2}", s.Sno, s.Cno, s.Degree); 172 } 173 174 Console.WriteLine(""); 175 Console.WriteLine("10.--------【条件查询-或查询】查询student表中95031班或性别为女的同学记录--------"); 176 var query10 = from s in studentList 177 where s.Class.Equals("95031") || s.Ssex.Equals("女") 178 select s; 179 foreach (var s in query10) 180 { 181 Console.WriteLine("Sno:{0},Sname:{1},Ssex:{2},Class:{3}", s.Sno, s.Sname, s.Ssex, s.Class); 182 } 183 184 185 Console.WriteLine(""); 186 Console.WriteLine(""); 187 188 Console.WriteLine("11.--------【排序】以Class降序descending查询Student表的所有记录--------"); 189 190 var query11 = from s in studentList 191 orderby s.Class descending 192 select s; 193 foreach (var s in query11) 194 { 195 Console.WriteLine("Sno:{0},Sname:{1},Ssex:{2},Class:{3}", s.Sno, s.Sname, s.Ssex, s.Class); 196 } 197 198 Console.WriteLine(""); 199 Console.WriteLine(""); 200 Console.WriteLine("12.--------【两个条件的排序】以Cno升序、Degree降序查询Score表的所有记录--------"); 201 202 var query12 = from s in scoreList 203 orderby s.Cno ascending, s.Degree descending 204 select s; 205 foreach (var s in query12) 206 { 207 Console.WriteLine("Sno:{0},Cno:{1},Degree:{2}", s.Sno, s.Cno, s.Degree); 208 } 209 210 Console.WriteLine(""); 211 Console.WriteLine(""); 212 Console.WriteLine("13.--------【聚合函数的应用】查询 95031 班的学生人数--------"); 213 214 var query13 = (from s in studentList 215 where s.Class.Equals("95031") 216 select s).Count(); 217 218 Console.WriteLine("95031 班的学生人数:{0}", query13); 219 220 Console.WriteLine(""); 221 Console.WriteLine("14.--------【聚合函数】查询'3-105'号课程的平均分--------"); 222 223 var query14 = (from s in scoreList 224 where s.Cno.Equals("3-105") 225 select s.Degree).Average(); 226 227 Console.WriteLine("'3-105'号课程的平均分:{0}", query14); 228 229 230 Console.WriteLine(""); 231 Console.WriteLine("15.--------【聚合函数】在学生表中求女生占总人数的百分比--------"); 232 233 var query15 = studentList.Count(x => x.Ssex.Equals("女"))*1.0 / studentList.Count(); 234 235 Console.WriteLine("女生占总人数的百分比:{0}", query15.ToString("p2")); 236 237 ///* 238 //字符型转换 转为字符串 239 //12345.ToString("n"); //生成 12,345.00 240 //12345.ToString("C"); //生成 ¥12,345.00 241 //12345.ToString("e"); //生成 1.234500e+004 242 //12345.ToString("f4"); //生成 12345.0000 243 //12345.ToString("x"); //生成 3039 (16进制) 244 //12345.ToString("p"); //生成 1,234,500.00% 245 //*/ 246 247 Console.WriteLine(""); 248 Console.WriteLine("16.--------【连接查询,join】查询所有学生的Sname、Cno和Degree列--------"); 249 250 //方案一 251 //var query16 = from s in studentList 252 // join sc in scoreList on s.Sno equals sc.Sno 253 // select new { s.Sname, sc.Cno, sc.Degree }; 254 255 //方案二 256 var query16=from s in studentList 257 from sc in scoreList 258 where s.Sno == sc.Sno 259 select new { s.Sname, sc.Cno, sc.Degree }; 260 261 262 foreach(var s in query16) 263 { 264 Console.WriteLine("Sname:{0}, Cno:{1}, Degree:{2}", s.Sname, s.Cno, s.Degree); 265 } 266 267 Console.WriteLine(""); 268 Console.WriteLine("17.--------【连接查询 where连接】查询所有学生的Sname、Cname和Degree列--------"); 269 270 var query17 = from s in studentList 271 from sc in scoreList 272 from c in courseList 273 where s.Sno == sc.Sno && sc.Cno == c.Cno 274 select new { s.Sname, c.Cname, sc.Degree }; 275 276 foreach(var s in query17) 277 { 278 Console.WriteLine("Sname:{0}, Cname:{1}, Degree:{2}", s.Sname, s.Cname, s.Degree); 279 } 280 281 282 //let子句用于在LINQ表达式中存储子表达式的计算结果。let子句创建一个范围变量来存储结果,变量被创建后, 283 //不能修改或把其他表达式的结果重新赋值给它。此范围变量可以在后续的LINQ子句中使用。 284 Console.WriteLine(""); 285 Console.WriteLine("18.--------【聚合 函数的应用】查询Score表中的最高分的学生学号和课程号--------"); 286 287 var query18 = from s in scoreList 288 let scoreMax = (from u in scoreList 289 select u.Degree).Max() 290 where s.Degree == scoreMax 291 select new { s.Sno, s.Cno }; 292 foreach(var s in query18) 293 { 294 Console.WriteLine("sno:{0} cno:{1}",s.Sno,s.Cno); 295 } 296 297 298 ////在Linq表达式中,into子句可以创建一个临时标识符,使用该标识符可以存储group、join或select子句的结果。 299 Console.WriteLine(""); 300 Console.WriteLine("19.-------【分组查询】-查询Score表中至少有5名学生选修的并以3开头的课程的平均分数--------"); 301 var query19=from s in scoreList 302 where s.Cno.StartsWith("3") 303 group s by s.Cno 304 into cc 305 where cc.Count()>=5 306 select new {cno=cc.Key,avg=cc.Average(x=>x.Degree)}; 307 308 foreach(var s in query19) 309 { 310 Console.WriteLine("cno:{0} avg:{1}",s.cno,s.avg); 311 } 312 313 Console.WriteLine(""); 314 Console.WriteLine("20.--------【聚合函数的应用】查询最低分大于70,最高分小于90的Sno列--------"); 315 var query20 = from s in scoreList 316 group s by s.Sno 317 into cc 318 where cc.Min(x => x.Degree) >= 70 && cc.Max(x => x.Degree) <= 90 319 select new { Sno = cc.Key }; 320 321 foreach(var s in query20) 322 { 323 Console.WriteLine("sno:{0}",s.Sno); 324 } 325 326 Console.WriteLine("21.---------统计总成绩大于170的学生"); 327 var query21 = from s in scoreList 328 group s by s.Sno 329 into cc 330 where cc.Sum(x => x.Degree) > 170 331 select new { sno = cc.Key }; 332 333 var query22 = from s in query21 334 from st in studentList 335 where s.sno == st.Sno 336 select new { st.Sno, st.Sname, st.Class, st.Ssex }; 337 foreach(var s in query22) 338 { 339 Console.WriteLine("Sno:{0},Sname:{1},Class:{2},Ssex:{3}", s.Sno, s.Sname, s.Class, s.Ssex); 340 } 341 342 343 Console.Read(); 344 } 345 } 346 }