C中的LINQ

C中的LINQ一 什么是 LINQ 二 用法 where 查询特定条件 usingSystem usingSystem Collections Generic usingSystem Linq classMainCla staticList Person personList newList Person newPerson Id 1 Name 小明 Age 20 Score 100 Person Person

一:什么是LINQ

LINQ代表语言集成查询,是.net框架的扩展,它允许我们用SQL查询数据库的方式来查询数据的集合


二:LINQ延迟查询的特性

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
   
     personList = new List 
    
      () { new Person(){ Id=1,Name="小明",Age=20,Score=100}, new Person(){ Id=2,Name="小王",Age=40,Score=80}, new Person(){ Id=3,Name="小刘",Age=60,Score=60}, }; static void Main(string[] args) { var list = personList.Where((p) => p.Age < 30); foreach (var temp in list) { Console.WriteLine(temp); } personList.Add(new Person() { Id = 4, Name = "小赵", Age = 15, Score = 100 }); foreach (var temp in list) { Console.WriteLine(temp); } } } class Person { public int Id; public string Name; public int Age; public int Score; public override string ToString() { return Id + "," + Name + "," + Age + "," + Score; } } 
     
   

三:扩展方法用法

Linq有两种写法,查询表达式写法(from…..in…..)和扩展方法写法,两种方法都是相互兼容的,程序编译时会将查询表达式转换为扩展方法,只要实现了IEnumerable的接口就可以使用Linq的扩展方法

——Select:返回指定类型

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
    
      personList = new List 
     
       () { new Person(){ Id=1,Name="小明",Age=20,Score=100}, new Person(){ Id=2,Name="小王",Age=40,Score=80}, new Person(){ Id=3,Name="小刘",Age=60,Score=60}, }; static void Main(string[] args) { var list = personList.Select(p => p.Name); var list = personList.Select(p => new { id = p.Id, name = p.Name }); foreach (var temp in list) { Console.WriteLine(temp); } } } class Person { public int Id; public string Name; public int Age; public int Score; public override string ToString() { return Id + "," + Name + "," + Age + "," + Score; } } 
      
    

——Where:查询特定条件

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
    
      personList = new List 
     
       () { new Person(){ Id=1,Name="小明",Age=20,Score=100}, new Person(){ Id=2,Name="小王",Age=40,Score=80}, new Person(){ Id=3,Name="小刘",Age=60,Score=60}, }; static void Main(string[] args) { var list = personList.Where((p) => p.Score >= 80 && p.Age < 50); foreach (var temp in list) { Console.WriteLine(temp); } } } class Person { public int Id; public string Name; public int Age; public int Score; public override string ToString() { return Id + "," + Name + "," + Age + "," + Score; } } 
      
    

——OfType:查询特定数据类型 

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List objList = new List() { "test1", 1, 1.34f, "test2", }; static void Main(string[] args) { var list = objList.OfType 
      
        (); foreach (var temp in list) { Console.WriteLine(temp); } } } 
      

——Join:将一个集合与另一个集合通过指定键合并,返回合并后的集合

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
    
      personList = new List 
     
       () { new Person(){ Id=1,Name="小明",Age=20,Score=100}, new Person(){ Id=2,Name="小王",Age=40,Score=80}, new Person(){ Id=3,Name="小刘",Age=60,Score=60}, }; static List 
      
        rewardList = new List 
       
         () { new Reward(){ Id=1,Score=100,RewardName="奖励1"}, new Reward(){ Id=2,Score=80,RewardName="奖励2"}, new Reward(){ Id=3,Score=60,RewardName="奖励3"}, }; static void Main(string[] args) { var list = personList.Join(rewardList, p => p.Score, r => r.Score, (p, r) => new { pList = p, rList = r }); foreach (var temp in list) { Console.WriteLine(temp); } } } class Person { public int Id; public string Name; public int Age; public int Score; public override string ToString() { return Id + "," + Name + "," + Age + "," + Score; } } class Reward { public int Id; public int Score; public string RewardName; public override string ToString() { return Id + "," + Score + "," + RewardName; } } 
        
       
      
    

——GroupJoin: 将一个集合与另一个集合通过指定键分组

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
    
      personList = new List 
     
       () { new Person(){ Id=1,Name="小明",Age=20,Score=100}, new Person(){ Id=2,Name="小王",Age=40,Score=80}, new Person(){ Id=3,Name="小刘",Age=60,Score=60}, }; static List 
      
        rewardList = new List 
       
         () { new Reward(){ Id=1,Score=101,RewardName="奖励1"}, new Reward(){ Id=2,Score=80,RewardName="奖励2"}, new Reward(){ Id=3,Score=60,RewardName="奖励3"}, }; static void Main(string[] args) { var list = personList.GroupJoin(rewardList, p => p.Score, r => r.Score, (p, result) => new { pList = p, count = result.Count() }); foreach (var temp in list) { Console.WriteLine(temp); } } } class Person { public int Id; public string Name; public int Age; public int Score; public override string ToString() { return Id + "," + Name + "," + Age + "," + Score; } } class Reward { public int Id; public int Score; public string RewardName; public override string ToString() { return Id + "," + RewardName; } } 
        
       
      
    

——OrderBy:对集合排序,默认是从小到大排序

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
    
      personList = new List 
     
       () { new Person(){ Id=1,Name="小明",Age=20,Score=100}, new Person(){ Id=2,Name="小王",Age=60,Score=60}, new Person(){ Id=3,Name="小刘",Age=60,Score=80}, }; static void Main(string[] args) { var list = personList.OrderBy((p) => p.Age).ThenBy((p) => p.Score); foreach (var temp in list) { Console.WriteLine(temp); } } } class Person { public int Id; public string Name; public int Age; public int Score; public override string ToString() { return Id + "," + Name + "," + Age + "," + Score; } } 
      
    

——Reverse:反转集合中元素的顺序

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
    
      personList = new List 
     
       () { new Person(){ Id=1,Name="小明",Age=20,Score=100}, new Person(){ Id=2,Name="小王",Age=40,Score=80}, new Person(){ Id=3,Name="小刘",Age=60,Score=60}, new Person(){ Id=3,Name="小刘",Age=60,Score=60}, }; static void Main(string[] args) { var list = personList.AsEnumerable().Reverse(); foreach (var temp in list) { Console.WriteLine(temp); } } } 
      
    

——GroupBy:自身分组查询

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
    
      personList = new List 
     
       () { new Person(){ Id=1,Name="小明",Age=20,Score=100}, new Person(){ Id=2,Name="小王",Age=40,Score=80}, new Person(){ Id=3,Name="小刘",Age=60,Score=60}, new Person(){ Id=4,Name="小赵",Age=30,Score=60}, }; static void Main(string[] args) { var list = personList.GroupBy((p) => p.Score, (score, p) => new { score = score, count = p.Count() }); foreach (var temp in list) { Console.WriteLine(temp); } } } class Person { public int Id; public string Name; public int Age; public int Score; public override string ToString() { return Id + "," + Name + "," + Age + "," + Score; } } 
      
    

——Any和All:判断集合中是否满足某个/条件

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
    
      personList = new List 
     
       () { new Person(){ Id=1,Name="小明",Age=20,Score=100}, new Person(){ Id=2,Name="小王",Age=40,Score=80}, new Person(){ Id=3,Name="小刘",Age=60,Score=60}, }; static void Main(string[] args) { bool b1 = personList.Any((p) => p.Age < 50); bool b2 = personList.All((p) => p.Age < 50); Console.WriteLine(b1); Console.WriteLine(b2); } } class Person { public int Id; public string Name; public int Age; public int Score; public override string ToString() { return Id + "," + Name + "," + Age + "," + Score; } } 
      
    

——Skip:跳过指定个元素查询

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
    
      personList = new List 
     
       () { new Person(){ Id=1,Name="小明",Age=20,Score=100}, new Person(){ Id=2,Name="小王",Age=40,Score=80}, new Person(){ Id=3,Name="小刘",Age=60,Score=60}, }; static void Main(string[] args) { var list = personList.Skip(1); foreach (var temp in list) { Console.WriteLine(temp); } } } class Person { public int Id; public string Name; public int Age; public int Score; public override string ToString() { return Id + "," + Name + "," + Age + "," + Score; } } 
      
    

——Take:只查询指定个元素

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
    
      personList = new List 
     
       () { new Person(){ Id=1,Name="小明",Age=20,Score=100}, new Person(){ Id=2,Name="小王",Age=40,Score=80}, new Person(){ Id=3,Name="小刘",Age=60,Score=60}, }; static void Main(string[] args) { var list = personList.Take(2); foreach (var temp in list) { Console.WriteLine(temp); } } } class Person { public int Id; public string Name; public int Age; public int Score; public override string ToString() { return Id + "," + Name + "," + Age + "," + Score; } } 
      
    

——Sum、Average、Max、Min:计算集合中指定数字类型数据的总和、平均值、最大值、最小值

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
    
      personList = new List 
     
       () { new Person(){ Id=1,Name="小明",Age=20,Score=100}, new Person(){ Id=2,Name="小王",Age=40,Score=80}, new Person(){ Id=3,Name="小刘",Age=60,Score=60}, }; static void Main(string[] args) { var sum = personList.Sum((p) => p.Age); var avg = personList.Average((p) => p.Age); var max = personList.Max((p) => p.Age); var min = personList.Min((p) => p.Age); Console.WriteLine(sum); Console.WriteLine(avg); Console.WriteLine(max); Console.WriteLine(min); } } class Person { public int Id; public string Name; public int Age; public int Score; public override string ToString() { return Id + "," + Name + "," + Age + "," + Score; } } 
      
    

——Concat: 连接两个相同类型集合,合并为一个集合

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
    
      numList1 = new List 
     
       () { 1, 2, 3 }; static List 
      
        numList2= new List 
       
         () { 4, 5, 6 }; static void Main(string[] args) { var list = numList1.Concat(numList2); foreach (var temp in list) { Console.WriteLine(temp); } } } 
        
       
      
    

——Distinct:从集合中去除掉重复的元素

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
    
      numList = new List 
     
       () { 1, 2, 3, 1, 2, 3 }; static void Main(string[] args) { var list = numList.Distinct(); foreach (var temp in list) { Console.WriteLine(temp); } } } 
      
    

使用Distinct去重类中某个字段需要实现IEqualityComparer接口

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
     
       personList = new List 
      
        () { new Person(){ Id=1,Name="小明",Age=20,Score=100}, new Person(){ Id=2,Name="小王",Age=40,Score=80}, new Person(){ Id=3,Name="小刘",Age=60,Score=60}, new Person(){ Id=3,Name="小刘",Age=60,Score=60}, }; static void Main(string[] args) { var list = personList.Distinct(new Person()); foreach (var temp in list) { Console.WriteLine(temp); } } } class Person : IEqualityComparer 
       
         { public int Id; public string Name; public int Age; public int Score; public new bool Equals(Person x, Person y) { if (x == null || y == null) { return false; } if (x.Age == y.Age) { return true; } return false; } public int GetHashCode(Person obj) { return obj.Age.GetHashCode(); } public override string ToString() { return Id + "," + Name + "," + Age + "," + Score; } } 
        
       
     

——ElementAt:得到集合中指定索引的元素,与[]作用相同

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
    
      numList = new List 
     
       () { 1, 2, 3}; static void Main(string[] args) { var value = numList.ElementAt(2); Console.WriteLine(value); } } 
      
    

 ——Count:得到集合中满足指定条件的元素个数

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
    
      numList1 = new List 
     
       () { 1, 2, 3 }; static void Main(string[] args) { var count = numList1.Count((n) => n >= 2); Console.WriteLine(count); } } 
      
    

——First/Single和Last:得到集合中第一个/最后一个元素(如果集合中包含多个元素,使用Single会报错)

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
    
      numList1 = new List 
     
       () { 1, 2, 3 }; static void Main(string[] args) { var value1 = numList1.First((n) => n >= 2); var value2 = numList1.Last((n) => n < 3); Console.WriteLine(value1); Console.WriteLine(value2); } } 
      
    

——ToDictionary:将集合转换为字典 

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
    
      personList = new List 
     
       () { new Person(){ Id=1,Name="小明",Age=20,Score=100}, new Person(){ Id=2,Name="小王",Age=40,Score=80}, new Person(){ Id=3,Name="小刘",Age=60,Score=60}, }; static void Main(string[] args) { var list = personList.ToDictionary(p => p.Id); foreach (var temp in list) { Console.WriteLine(temp); } } } class Person { public int Id; public string Name; public int Age; public int Score; public override string ToString() { return Id + "," + Name + "," + Age + "," + Score; } } 
      
    

——ToList: 将集合转换为list

using System; using System.Collections.Generic; using System.Linq; class MainClass { static List 
    
      personList = new List 
     
       () { new Person(){ Id=1,Name="小明",Age=20,Score=100}, new Person(){ Id=2,Name="小王",Age=40,Score=80}, new Person(){ Id=3,Name="小刘",Age=60,Score=60}, }; static void Main(string[] args) { var dict = personList.ToDictionary(p => p.Id); List 
      
        list = dict.Values.ToList(); foreach (var temp in list) { Console.WriteLine(temp); } } } class Person { public int Id; public string Name; public int Age; public int Score; public override string ToString() { return Id + "," + Name + "," + Age + "," + Score; } } 
       
      
    

using System; using System.Collections.Generic; using System.Linq; class MainClass { static void Main(string[] args) { List 
    
      list1 = new List 
     
       () { 1, 2, 3 }; List 
      
        list2 = new List 
       
         () { 3, 1, 2 }; list1 = list1.OrderBy(temp => temp).ToList(); list2 = list2.OrderBy(temp => temp).ToList(); Console.WriteLine(list1.SequenceEqual(list2)); } } 
        
       
      
    

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/211134.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午11:08
下一篇 2026年3月18日 下午11:08


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号