C# LINQ

2021-04-18 16:28

阅读:530

标签:return   ogr   bsp   amp   col   foreach   into   style   group   

1. 自定义 Master,Kongfu 类

 1 class Master
 2 {
 3 
 4     public int Id { get; set; }
 5     public string Name { get; set; }
 6     public int Age { get; set; }
 7     public string MenPai { get; set; }
 8     public string Kongfu { get; set; }
 9     public int Level { get; set; }
10 
11     public override string ToString()
12     {
13         return string.Format("Id:{0},Name:{1},Age:{2},MenPai:{3},Kongfu:{4},Level:{5}", Id, Name, Age,MenPai,Kongfu,Level);
14     }
15 }

 

 1 class Kongfu
 2 {
 3     public int Id { get; set; }
 4     public string Name { get; set; }
 5     public int Power { get; set; }
 6 
 7     public override string ToString()
 8     {
 9         return string.Format("Id:{0},Name:{1},Power:{2}", Id, Name, Power);
10     }
11 }

 

 

2. 测试代码

  1 class Program
  2 {
  3     static void Main(string[] args)
  4     {
  5         var master = new List()
  6         {
  7             new Master() { Id = 100, Age = 100, Kongfu = "九阳神功",Level=10,MenPai="华山",Name="李二狗" },
  8             new Master() { Id = 50, Age = 10, Kongfu = "九阳神功1" ,Level=5,MenPai="华山",Name="李大狗"},
  9             new Master() { Id = 200, Age = 60, Kongfu = "九阳神功2",Level=6,MenPai="华山",Name="李狗蛋" },
 10         };
 11 
 12         var kongfu = new List()
 13         {
 14             new Kongfu(){Id=1,Name="打狗棒法",Power=90},
 15             new Kongfu(){Id=2,Name="打狗棒法1",Power=30},
 16             new Kongfu(){Id=3,Name="打狗棒法2",Power=20},
 17         };
 18           
 19         //使用LINQ做查询(表达式写法)
 20         var res1 = from m in master   //from后面设置查询的集合
 21                     where m.Level > 8     //where跟上条件
 22 
 23                     select m;//表示m的结果
 24         //select m.Name;  //只输出名字
 25 
 26       
 27         //多个条件
 28         var res3 = from m in master
 29 
 30                     where m.Level > 8 && m.MenPai == "丐帮"
 31 
 32                     select m;
 33 
 34         var res4 = master.Where(m => m.Level > 8 && m.MenPai == "丐帮");
 35 
 36         //LINQ联合查询
 37         var res5 = from m in master
 38                     from k in kongfu
 39                     where m.Kongfu==k.Name
 40                     select new { master = m, kongfu = k };
 41                     //select m;
 42         foreach (var temp in res)
 43         {
 44             Console.WriteLine(temp);
 45         }
 46 
 47         //join on 联合查询
 48         var res12 = from m in master
 49                     join k in kongfu on m.Kongfu equals k.Name
 50                     where k.Power>90
 51                     select new { master = m, kongfu = k };
 52 
 53 
 54         //对查询结果升序排序
 55         var res7 = from m in master
 56 
 57                     where m.Level > 8 && m.MenPai == "丐帮"
 58                     orderby m.Age
 59 
 60                     select m;
 61 
 62         //对查询结果倒序排序
 63         var res8 = from m in master
 64 
 65                     where m.Level > 8 && m.MenPai == "丐帮"
 66                     orderby m.Age descending
 67 
 68                     select m;
 69 
 70         //对查询结果升序排序
 71         var res9 = from m in master
 72 
 73                     where m.Level > 8 && m.MenPai == "丐帮"
 74                     orderby m.Age,m.Level //按照多个字段进行排序,如果字段的属性相同,就按照第二个属性排序
 75 
 76                     select m;
 77 
 78         var res10 = master.Where(m => m.Level > 8 && m.MenPai == "丐帮").OrderBy(m => m.Age);
 79         var res11 = master.Where(m => m.Level > 8 && m.MenPai == "丐帮").OrderBy(m => m.Age).ThenBy(m=>m.Level);
 80 
 81 
 82 
 83         //把人物按照所学功夫进行分类,看下哪个功夫修炼人数最多(分组查询)
 84         var res13 = from k in kongfu
 85                     join m in master on k.Name equals m.Kongfu
 86                     into groups
 87                     orderby groups.Count()
 88                     select new { kongfu = k,count=groups.Count()};
 89 
 90         //按照字段分组
 91         var res14 = from m in master
 92                     group m by m.MenPai
 93                     into g
 94                     select new { count=g.Count(),key=g.Key}; //g.Key按照哪个属性分组
 95 
 96         //量词操作符any all判断集合中是否满足某个条件
 97         bool res15=master.Any(m => m.MenPai == "丐帮");
 98         Console.WriteLine(res15);//只要有一个满足返回true
 99 
100         bool res16=master.All(m => m.MenPai == "丐帮");//所有都满足才返回true
101 
102 
103 
104         Console.ReadKey();
105     }
106 
107     //过滤方法
108     static bool Test1(Master m)
109     {
110         if (m.Level > 8) return true;
111         return false;
112     }
113 }

 

C# LINQ

标签:return   ogr   bsp   amp   col   foreach   into   style   group   

原文地址:https://www.cnblogs.com/coderJiebao/p/CSharp02.html


评论


亲,登录后才可以留言!