什么是LINQ:LINQ 是一组 .NET Framework 扩展模块集合,内含语言集成查询、集合以及转换操作。它使用查询的本机语言语法来扩展 C# 和 Visual Basic,并提供利用这些功能的类库。
什么是LINQ 101:是学习LINQ的不错的资源(下载地址:http://pan.baidu.com/s/1ntE74NJ),当初我也是看LINQ101来熟悉LINQ的。现把我当时的学习笔记整理出来
Restriction (约束)
Projection (投影)
Ordering (排序)
Partitioning (分区)
Join
Aggregate(聚合)
Grouping(分组)
Set(集合)
Conversion(转换)
Element(元素操作)
本篇文章介绍前三个
一、 约束 ——where
var query = from item in [集合] where [item满足的条件] select item;
int[] numbers = { 5, 4, 3, 7, 9, -12 }; var query = from n in numbers where n % 3 == 0 select n; Console.WriteLine("被3整除的数:"); foreach (var num in numbers) { Console.WriteLine(num); }
多个Where 条件且筛选
1 var query = from item in sources 2 where (字段1的条件1) || (字段1的条件2) 3 where (字段2的条件1) || (字段2的条件2) 4 select new Entity .... 5 6 如 7 var query = from item in sources 8 where (string.IsNullOrEmpty(txtSearch.Text.Trim())) || (item.Filed1.Contains(txtSearch.Text.Trim())) 9 where (string.IsNullOrEmpty(txtFilter.Text.Trim())) || (item.Filed2.Contains(txtFilter.Text.Trim())) 10 ... 11 select new Entity ....
二、投影——select
例1:让数组中每个数都加1
1 int[] numbers = { 5, 4, 1, 3, 9, 8,-13 }; 2 3 var numsPlusOne = 4 from n in numbers 5 select n + 1; 6 7 Console.WriteLine("整数+1:"); 8 foreach (var num in numsPlusOne) 9 { 10 Console.WriteLine(num); 11 }
例2:选取某类中一个属性
1 public class Config 2 { 3 public Guid ID { get; set; } 4 public string Name { get; set; } 5 public string Remark { get; set; } 6 7 Config() { } 8 9 public static List<Config> GetConfigs() 10 { 11 return new List<Config> 12 { 13 new Config{ ID=Guid.NewGuid(), Name="XX1号",Remark="Remark1号"}, 14 new Config{ ID=Guid.NewGuid(), Name="XX2号",Remark="Remark2号"}, 15 new Config{ ID=Guid.NewGuid(), Name="XX3号",Remark="Remark3号"} 16 }; 17 } 18 } 19 20 21 static void Linq3() 22 { 23 var source = Config.GetConfigs(); 24 var query = from item in source 25 select item.Name; 26 foreach (var c in query) 27 { 28 Console.WriteLine(c); 29 } 30 }
例3:数组下标转换
1 static void Linq4() 2 { 3 int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; 4 string[] strings = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; 5 6 var textNums = 7 from n in numbers 8 select strings[n]; 9 10 Console.WriteLine("数组下标转换"); 11 foreach (var s in textNums) 12 { 13 Console.WriteLine(s); 14 } 15 }
例3:匿名类型,大小写
1 static void Linq5() 2 { 3 string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" }; 4 var query = from word in words 5 select new { Upper = word.ToUpper(), Lower = word.ToLower() }; 6 foreach (var item in query) 7 { 8 Console.WriteLine("大写: {0} 小写: {1}",item.Upper,item.Lower); 9 } 10 }
例4:将投影的属性放到一个类中
1 public class Dto 2 { 3 public string Field1 { get; set; } 4 public string Field2 { get; set; } 5 public string Field3 { get; set; } 6 } 7 8 static void Linq6() 9 { 10 var source = Config.GetConfigs(); 11 var query = from item in source 12 select new Dto { Field1 = item.ID.ToString(), Field2 = item.Name, Field3 = item.Remark }; 13 foreach (var item in query) 14 { 15 Console.WriteLine("{0} {1} {2}", item.Field1, item.Field2, item.Field3); 16 } 17 }
例5:筛选 加where条件参见 where 例1
例6:复合from子句(选取座机号码区号为021的客户信息)
1 public class Guest 2 { 3 /// <summary> 4 /// 姓名 5 /// </summary> 6 public string Name { get; set; } 7 8 /// <summary> 9 /// 年龄 10 /// </summary> 11 public int Age { get; set; } 12 13 /// <summary> 14 /// 电话表 15 /// </summary> 16 public List<string> TelTable { get; set; } 17 } 18 19 20 static void Linq8() 21 { 22 var source = new List<Guest> 23 { 24 new Guest{ Name="张三",Age=21,TelTable=new List<string>{"010-123456","13000000"}}, 25 new Guest{ Name="李四",Age=22,TelTable=new List<string>{"021-123456","13100000"}}, 26 new Guest{ Name="王五",Age=23,TelTable=new List<string>{"022-123456","13200000"}} 27 }; 28 // 找电话簿中是021区号的客户 29 var query = from guest in source 30 from tel in guest.TelTable 31 where tel.Contains("021-") 32 select guest; 33 34 foreach (var guest in query) 35 { 36 Console.WriteLine("{0} 年龄:{1}", guest.Name, guest.Age); 37 foreach (var tel in guest.TelTable) { Console.WriteLine(" 电话:{0}",tel); } 38 } 39 }
例7:多个 form 子句(交叉连接)
1 static void Linq7() 2 { 3 int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; 4 int[] numbersB = { 1, 3, 5, 7, 8 }; 5 6 var pairs = 7 from a in numbersA 8 from b in numbersB 9 where a < b 10 select new { a, b }; 11 12 Console.WriteLine("交叉连接 a < b:"); 13 foreach (var pair in pairs) 14 { 15 Console.WriteLine("{0} 小于 {1}", pair.a, pair.b); 16 } 17 }
三、排序
orderby 排序字段
orderby 排序字段 descending
Thenby(orderby 多个排序字段)
ThenByDescending (orderby 多个排序字段 descending)
例1:orderby 排序字段
1 static void Linq9() 2 { 3 string[] words = { "cherry", "apple", "blueberry" }; 4 5 var sortedWords = 6 from w in words 7 orderby w 8 select w; 9 10 Console.WriteLine("单词排序 order by 单个排序字段:"); 11 foreach (var w in sortedWords) 12 { 13 Console.WriteLine(w); 14 } 15 }
例2:orderby 排序字段 descending
1 static void Linq10() 2 { 3 string[] words = { "cherry", "apple", "blueberry" }; 4 5 var sortedWords = 6 from w in words 7 orderby w descending 8 select w; 9 10 Console.WriteLine("单词排序 order by 单个排序字段 从大到小:"); 11 foreach (var w in sortedWords) 12 { 13 Console.WriteLine(w); 14 } 15 }
例3:Thenby(orderby 多个排序字段)
1 static void Linq11() 2 { 3 string[] words = { "cherry", "appppp", "cherryo", "appppppple", "blueberry" }; 4 5 // 先按名称排,再按长度排序 6 var sortedWords = 7 from w in words 8 orderby w,w.Length 9 select w; 10 11 Console.WriteLine("单词排序 ThenBy 多个排序字段"); 12 foreach (var w in sortedWords) 13 { 14 Console.WriteLine(w); 15 } 16 }
例4:ThenByDescending (orderby 多个排序字段 descending)
注意主要条件和次要的升降序不传递
1 static void Linq12() 2 { 3 string[] words = { "cherry", "appppp", "cherryo", "appppppple", "blueberry" }; 4 5 // 先按名称排,再按长度排序 6 var sortedWords = 7 from w in words 8 orderby w descending, w.Length descending 9 select w; 10 11 Console.WriteLine("单词排序 ThenBy 多个排序字段"); 12 foreach (var w in sortedWords) 13 { 14 Console.WriteLine(w); 15 } 16 }