好久之前就想系统的学习下LINQ,好久之前……
本篇文章主要介绍LINQ等的标准查询操作符,内容取自《LINQ高级编程》,后续还会介绍LINQ to XML ,LINQ to SQL。
进入正题
(一)先介绍下标准查询操作符的概念
它是一系列方法的集合,如常见的Select 、Where、OrderBy等,这些方法作用于序列(集合)之上,序列是一个实现了IEnumerable<T>接口或IQueryable<T>接口的类型的对象。
IEnumerable和IQueryable的一个区别是:IEnumerable接口提供了向前遍历的功能。它不具备在各个数据项之间移动(向前移动除外)的能力。IQueryalbe可以使查询操作更具灵活性。
通俗的讲查询操作符是用好linq的基本,就像砍材要用的刀一样……
(二)另外再说下 查询语法和方法语法的概念
查询语法 即用查询表达式来编写查询。 如代码:
1 int[] data={1,2,3,4}; 2 IEnumerable<int> query = from da in data where da > 2 select da;
那方法语法呢 如代码:
1 int[] data={1,2,3,4}; 2 IEnumerable<int> query2 = data.Select(o => o).Where(d => d > 2);
LINQ在编译的时候,查询语法将会转为方法语法。相对而言,查询语法更容易阅读、理解和维护
(三)各种查询操作符汇总列表
标准查询操作符 (方法语法) | 查询表达式(查询语法) |
All(Of T) | N/A |
Any | N/A |
Average | N/A |
Cast(Of T) | An explicit range of variables |
Count | N/A |
Distinct | N/A |
GroupBy | group by |
GroupJoin | join…in…on…into |
Join | join…in…on…equals… |
LongCount | N/A |
Max | N/A |
Min | N/A |
OrderBy | order by |
OrderByDescending | order by descending |
Select | select |
SelectMany | Multiple from clauses |
Skip | N/A |
SkipWhile | N/A |
Sum | N/A |
Take | N/A |
TakeWhile | N/A |
ThenBy | order by |
ThenByDescending | order by descending |
Where | where |
备注:N/A (Not Applicable 不适用的)
(四) 下面主要通过代码来讲解各个标准查询操作符。
1. 投影操作符 (Select SelectMany)
1 #region 投影操作符 将序列中的元素转换为一个由开发人员定义的形式的操作 2 /// <summary> 3 /// 对单个序列或集合中的值进行投影 4 /// </summary> 5 static void SelectFunction() 6 { 7 DataContext contexts = new DataContext("Initial Catalog=AdventureWorks; Integrated Security=true"); 8 Table<Contact> contacts = contexts.GetTable<Contact>(); 9 //查询语法 10 var query = from c in contacts where c.FirstName == "gong" select new { c.FirstName }; 11 //方法语法 12 query = contacts.Select(o => new { o.FirstName }).Where(c => c.FirstName == "gong"); 13 foreach (var item in query) 14 { 15 Console.WriteLine(item.FirstName); 16 } 17 } 18 //output: 19 //Gong 20 //Gong 21 class name{ public List<string> pets; public string FirstName;} 22 /// <summary> 23 /// selectMany 提供了将多个from子句组合起来的功能,它将每个对象的结果合并成单个序列 24 /// </summary> 25 static void SelectManyFunction( ) 26 { 27 //List<name> list =new List<name>() { 28 name[] list =new name[] { 29 new name{ FirstName="Gwt",pets=new List<string>{"TEST"} } , 30 new name{ FirstName="Gwt1",pets=new List<string>{"TEST4","Test41"} }, 31 }; 32 //将所有List<string> 合并 33 IEnumerable<string> query = list.SelectMany(own => own.pets); 34 foreach (string item in query) 35 { 36 Console.Write(item +" "); 37 } 38 Console.WriteLine(""); 39 //下面这个循环等于上面的seletmany方法 40 var query2 = list.Select(own => own.pets); 41 foreach (var item in query2) 42 { 43 foreach (var item2 in item) 44 { 45 Console.Write(item2+" "); 46 } 47 } 48 } 49 //output: 50 // TEST TEST4 TEST41 51 // TEST TEST4 TEST41
2. 限制操作符 (Where)
1 #region 限制操作符 2 /// <summary> 3 /// where操作符不启动查询的执行,当开始对对象进行枚举时查询才开始执行 4 /// </summary> 5 static void WhereFunction() 6 { 7 int[] numbers = { 1,2,3,4,5,6}; 8 IEnumerable<int> query = numbers.Select(o => o).Where(o => o > 3); 9 foreach (int item in query) 10 { 11 Console.Write(item+" "); 12 } 13 } 14 //output; 15 //4 5 6 16 #endregion
3. 排序操作符 (OrderBy OrderByDescending ThenBy ThenByDescending Reverse)
1 #region 排序操作符 2 /// <summary> 3 /// OrderBy/OrderByDescending 对应提供了按照升序/降序的方式对结果进行排序的功能 4 /// ThenBy/ThenByDescending 实现按照次关键字对序列进行升序/降序排列 5 /// </summary> 6 static void OrderByFunction() 7 { 8 DataContext context = new DataContext(StaticData.ConnectSql); 9 Table<Contact> contacts = context.GetTable<Contact>( ); 10 IEnumerable<Contact> query = contacts.Select(o => o).OrderBy(o => o.FirstName).ThenBy(o => o.LastName); 11 12 foreach (Contact item in query) 13 { 14 Console.WriteLine(item.FirstName + " " + item.LastName); 15 } 16 Console.WriteLine(); 17 query = contacts.Select(o => o).OrderByDescending(o => o.FirstName).ThenByDescending(o => o.LastName); 18 foreach (Contact item in query) 19 { 20 Console.WriteLine(item.FirstName + " " + item.LastName); 21 } 22 } 23 //output: 24 //Gong tao 25 //Gong taos 26 //gong2 tao2 27 //gong3 tao3 28 //gong4 tao4 29 //gong5 tao5 30 // 31 //gong5 tao5 32 //gong4 tao4 33 //gong3 tao3 34 //gong2 tao2 35 //Gong taos 36 //Gong tao 37 /// <summary> 38 /// Reverse 把数据源的数据按相反的顺序返回 39 /// </summary> 40 static void ReverseFunction() 41 { 42 int[] numbers = { 1,2,3,4,5}; 43 IEnumerable<int> query = numbers.Reverse(); 44 foreach (int item in query) 45 { 46 Console.Write(item+" "); 47 } 48 } 49 //output: 50 //5 4 3 2 1 51 #endregion
4. 联接操作符 (Join GroupJoin)
1 #region 联接操作符 2 /// <summary> 3 /// join 相当于T-SQL中的inner join 4 /// </summary> 5 static void JoinFunction() 6 { 7 DataContext context = new DataContext(StaticData.ConnectSql); 8 Table<Contact> contact = context.GetTable<Contact>(); 9 Table<Employee> employee = context.GetTable<Employee>(); 10 //方法语法 11 var query = contact.Join(employee, con => con.ContactID, emp => emp.ContactID, 12 (con, emp) => new { ContactName = con.FirstName, emp }) 13 .Where(con=>con.ContactName=="gong2"); 14 //查询语法 15 query = from con in contact 16 join emp in employee on con.ContactID equals emp.ContactID 17 where con.FirstName=="gong2" 18 select new { ContactName = con.FirstName, emp } ; 19 foreach (var item in query) 20 { 21 Console.WriteLine(item.ContactName + " " + item.emp.EmployeeID); 22 } 23 } 24 //output: 25 //gong2 23 26 //gong2 24 27 //gong2 25 28 //gong2 26 29 class Team 30 { 31 public string TeamName; 32 } 33 class Rider 34 { 35 public string Name; 36 public string TeamName; 37 } 38 /// <summary> 39 /// GroupJoin 40 /// </summary> 41 static void GroupJoinFunction() 42 { 43 List<Team> teams = new List<Team>{new Team{ TeamName="t1"} 44 ,new Team{TeamName="t2"} 45 ,new Team{TeamName="t3"} 46 }; 47 List<Rider> riders = new List<Rider> { 48 new Rider{Name="r1",TeamName="t1"}, 49 new Rider{Name="r2",TeamName="t1"}, 50 new Rider{Name="r3",TeamName="t2"}, 51 new Rider{Name="r4",TeamName="t2"}, 52 new Rider{Name="r5",TeamName="t3"}, 53 new Rider{Name="r6",TeamName="t3"}, 54 }; 55 //方法语法 56 var query = teams.GroupJoin(riders, t => t.TeamName, r => r.TeamName, 57 (t, r) => new { teamname = t.TeamName, ridernames = r.Select(o => o.Name) }); 58 foreach (var item in query) 59 { 60 Console.WriteLine(item.teamname); 61 foreach (var item2 in item.ridernames) 62 { 63 Console.WriteLine(" "+item2); 64 } 65 } 66 } 67 //output: 68 //t1 69 // r1 70 // r2 71 //t2 72 // r3 73 // r4 74 //t3 75 // r5 76 // r6 77 #endregion
5. 分组操作符 (GroupBy)
1 #region 分组操作符 根据一个特定的值(选择器) 将序列中的值或元素进行分组 2 /// <summary> 3 /// 4 /// </summary> 5 static void GroupByFunction() 6 { 7 DataContext context = new DataContext(StaticData.ConnectSql); 8 Table<SalesOrderHeader> orders = context.GetTable<SalesOrderHeader>(); 9 //按照每个销售人员对订单进行分组 10 var query = orders.Where(o => o.SalesPersonID > 0).GroupBy(order => order.SalesPersonID, order => order.CustomerID); 11 12 foreach (var item in query) 13 { 14 Console.WriteLine(item.Key); 15 foreach (var item2 in item) 16 { 17 Console.WriteLine(" " + item2); 18 } 19 } 20 } 21 #endregion
6. 串联操作符 (Concat)
1 #region 串联操作符 2 /// <summary> 3 /// 串联操作符 Concat 【将两个集合合并成一个集合】 4 /// </summary> 5 static void ConcatFunction( ) 6 { 7 DataContext context = new DataContext(StaticData.ConnectSql); 8 Table<Contact> contacts = context.GetTable<Contact>( ); 9 Table<SalesOrderHeader> orders = context.GetTable<SalesOrderHeader>(); 10 //将Contact表的Firstname和SalesOrderHeader的CustomerID串联起来 11 var query = contacts.Select(con => con.FirstName).Concat(orders.Select(order => order.CustomerID.ToString())); 12 foreach (var item in query) 13 { 14 Console.WriteLine(item.ToString()); 15 } 16 } 17 #endregion
动动您的手指,点下支持,您的支持是我最大动力!