LINQ查询表达式---------select子句
LINQ查询表达式---------select子句
1.1常见的select子句查询
class Program { public class PerInfo { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public List<int> Scores { get; set; } } List<PerInfo> PerInfos = new List<PerInfo>() { new PerInfo { Id = 1, Name = "小白", Age = 15, Scores = new List<int>() { 78, 88, 95, 60 } }, new PerInfo { Id = 2, Name = "小日", Age = 18, Scores = new List<int>() { 98, 86, 79, 99 } }, new PerInfo { Id = 3, Name = "小月", Age = 21, Scores = new List<int>() { 95, 95, 96, 97 } }, new PerInfo { Id = 4, Name = "小明", Age = 19, Scores = new List<int>() { 96, 97, 98, 99 } } }; static void Main(string[] args) { Program p = new Program(); var query = from q in p.PerInfos select q; foreach (PerInfo per in query) { Console.WriteLine("姓名:{0},年龄:{1}", per.Name, per.Age); } Console.ReadLine(); } }
输出结果: 姓名:小白,年龄:15 姓名:小日,年龄:18 姓名:小月,年龄:21 姓名:小明,年龄:19
1.2 其他形式的select查询
class Program { public class PerInfo { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public List<int> Scores { get; set; } public CustrInfo GetCustrInfo(List<CustrInfo> p, int id) { CustrInfo cInfo = (from q in p where q.Id == id select q).FirstOrDefault(); return cInfo; } public override string ToString() { return "姓名:" + Name + " 年龄:" + Age; } } public class CustrInfo { public int Id { get; set; } public string Tel { get; set; } public string Email { get; set; } public override string ToString() { return "邮箱:" + Email + "电话:" + Tel; } } public class ScoreInfo { public double Average { get; set; } public int ID { get; set; } } static void Main(string[] args) { List<PerInfo> perInfos = new List<PerInfo>() { new PerInfo { Id = 1, Name = "小白", Age = 15, Scores = new List<int>() { 78, 88, 95, 60 } }, new PerInfo { Id = 2, Name = "小日", Age = 18, Scores = new List<int>() { 98, 86, 79, 99 } }, new PerInfo { Id = 3, Name = "小月", Age = 21, Scores = new List<int>() { 95, 95, 96, 97 } }, new PerInfo { Id = 4, Name = "小明", Age = 19, Scores = new List<int>() { 96, 97, 98, 99 } } }; List<CustrInfo> custrInfos = new List<CustrInfo>() { new CustrInfo {Id = 1, Tel = "110", Email = "Xuahau@123.Com"}, new CustrInfo {Id = 2, Tel = "120", Email = "xchads@qq.Com"}, new CustrInfo {Id = 3, Tel = "119", Email = "kashda@q56.Com"}, new CustrInfo {Id = 4, Tel = "999", Email = "sadhkuu@143.Com"} }; Program p = new Program(); //1 根据查询条件查询数据源 var query1 = from q in perInfos where q.Id > 1 select q; Console.WriteLine("Query1:"); foreach (PerInfo per in query1) { Console.WriteLine(per.ToString()); } //2 查询单个属性 var query2 = from q in perInfos where q.Id > 1 select q.Name; Console.WriteLine("\nQuery2:"); foreach (string s in query2) { Console.WriteLine(s); } //3 查询一个经过筛选的对象 var query3 = from q in perInfos where q.Id > 1 select q.GetCustrInfo(custrInfos, q.Id); Console.WriteLine("\nQuery3:"); foreach (CustrInfo c in query3) { Console.WriteLine(c.ToString()); } //4 查询内部数组 var query4 = from q in perInfos where q.Id > 1 select q.Scores[0]; Console.WriteLine("\nQuery4:"); foreach (int i in query4) { Console.WriteLine(i); } //5 表达式 var query5 = from q in perInfos where q.Id > 1 select q.Scores[0] * 1.1; Console.WriteLine("\nQuery5:"); foreach (int i in query5) { Console.WriteLine(i); } //6 求平均数 var query6 = from q in perInfos where q.Id > 1 select q.Scores.Average(); Console.WriteLine("\nQuery6:"); foreach (int i in query6) { Console.WriteLine(i); } //7 查询多个属性 var query7 = from q in perInfos where q.Id > 1 select new { q.Name, q.Age }; Console.WriteLine("\nQuery7:"); foreach (var item in query7) { Console.WriteLine(item.Name + " " + item.Age); } //8 对查询结果进行投影 var query8 = from q in perInfos where q.Id > 1 select new ScoreInfo { ID = q.Id, Average = q.Scores.Average() }; Console.WriteLine("\nQuery8:"); foreach (var item in query8) { Console.WriteLine("{0},{1}", item.ID, item.Average); } //9 查询平均分大于85的联系人信息 var query9 = from q in perInfos where q.Scores.Average() > 85 join ci in custrInfos on q.Id equals ci.Id select ci; Console.WriteLine("\nQuery9:"); foreach (var item in query9) { Console.WriteLine("{0} {1} {2}", item.Id, item.Email, item.Tel); } Console.ReadLine(); } }
输出结果: Query1: 姓名:小日 年龄:18 姓名:小月 年龄:21 姓名:小明 年龄:19 Query2: 小日 小月 小明 Query3: 邮箱:xchads@qq.Com电话:120 邮箱:kashda@q56.Com电话:119 邮箱:sadhkuu@143.Com电话:999 Query4: 98 95 96 Query5: 107 104 105 Query6: 90 95 97 Query7: 小日 18 小月 21 小明 19 Query8: 2,90.5 3,95.75 4,97.5 Query9: 2 xchads@qq.Com 120 3 kashda@q56.Com 119 4 sadhkuu@143.Com 999