文档:https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b
1.可以对查询出来的结果做一些转换,下面的例子在数组中查找以"B"开头的名字,然后全部转成小写输出
1 2 3 4 5 6 7 8 | string [] names = { "Jack" , "Bob" , "Bill" , "Catty" , "Willam" }; var rs = from n in names where n.StartsWith( "B" ) select n.ToLower(); foreach ( var item in rs) { Response.Write(item+ "<br />" ); } |
2.返回匿名类型,只返回需要的信息
1 2 3 4 5 6 7 | var users = from c in UserList where c.Mobile.StartsWith( "135" ) select new { Name = c.Name, Contact = c.Address + " " + c.Mobile }; |
3.使用原数组,产生两组新数组
1 2 3 4 5 6 7 8 9 10 11 12 13 | public void Linq9() { string [] words = { "aPPLE" , "BlUeBeRrY" , "cHeRry" }; var upperLowerWords = from w in words select new { Upper = w.ToUpper(), Lower = w.ToLower() }; foreach ( var ul in upperLowerWords) { Console.WriteLine( "Uppercase: {0}, Lowercase: {1}" , ul.Upper, ul.Lower); } } |
4.判断数字是否与他的下标一致
1 2 3 4 5 6 7 8 9 | int [] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var numsInPlace = numbers.Select((num, index) => new { Num = num, InPlace = (num == index) }); Console.WriteLine( "Number: In-place?" ); foreach ( var n in numsInPlace) { Response.Write( string .Format( "{0}: {1}<br />" , n.Num, n.InPlace)); } |
5.获得A小于B的所有可能的集合,返回 拿A中的每个元素和B中的每个元素对比的结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | int [] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; int [] numbersB = { 1, 3, 5, 7, 8 }; var pairs = from a in numbersA from b in numbersB where a < b select new { a, b }; Console.WriteLine( "Pairs where a < b:" ); foreach ( var pair in pairs) { Console.WriteLine( "{0} is less than {1}" , pair.a, pair.b); } |
6.从一个集合的子集中过滤,金额小于500的订单
1 2 3 4 5 6 7 | List<Customer> customers = GetCustomerList(); var orders = from c in customers from o in c.Orders where o.Total < 500.00 select new { c.CustomerID, o.OrderID, o.Total }; |
7.查询所有大于指定日期的用户订单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | List<Customer> customers = GetCustomerList(); var orders = from c in customers from o in c.Orders where o.OrderDate >= new DateTime(1998, 1, 1) select new { c.CustomerID, o.OrderID, o.OrderDate };延伸:List<Customer> customers = GetCustomerList(); DateTime cutoffDate = new DateTime(1997, 1, 1); var orders = from c in customers where c.Region == "WA" from o in c.Orders where o.OrderDate >= cutoffDate select new { c.CustomerID, o.OrderID }; |
8.代替二重for循环
参考:http://www.cnblogs.com/manupstairs/archive/2012/11/27/2790114.html
http://www.cnblogs.com/lyout/archive/2012/11/28/2792622.html
1 2 3 4 5 6 | List<Customer> customers = GetCustomerList(); var customerOrders = customers.SelectMany( (cust, custIndex) => cust.Orders.Select(o => "Customer #" + (custIndex + 1) + " has an order with OrderID " + o.OrderID)); |
9.Cross join 交叉连接查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public void Linq102() { string [] categories = new string []{ "Beverages" , "Condiments" , "Vegetables" , "Dairy Products" , "Seafood" }; List<Product> products = GetProductList(); var q = from c in categories join p in products on c equals p.Category select new { Category = c, p.ProductName }; foreach ( var v in q) { Console.WriteLine(v.ProductName + ": " + v.Category); } } |
1 2 3 4 5 6 7 8 9 | Result: Chai: Beverages Chang: Beverages Guaraná Fantástica: Beverages Sasquatch Ale: Beverages Steeleye Stout: Beverages Côte de Blaye: Beverages Chartreuse verte: Beverages Ipoh Coffee: Beverages |
10. Group join 分组查询;将数据按照不同的类型划分,归入到不同的组中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | public void Linq103() { string [] categories = new string []{ "Beverages" , "Condiments" , "Vegetables" , "Dairy Products" , "Seafood" }; List<Product> products = GetProductList(); var q = from c in categories join p in products on c equals p.Category into ps select new { Category = c, Products = ps }; foreach ( var v in q) { Console.WriteLine( "组" +v.Category + ":" ); foreach ( var p in v.Products) { Console.WriteLine( " " + p.ProductName); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Result: 组Beverages: Chai Chang 组Condiments: Aniseed Syrup Chef Anton's Cajun Seasoning Chef Anton's Gumbo Mix 组Vegetables: Dairy Products: Queso Cabrales |
11.Left Outer Join 左外连接查询,如果连接表没有数据,这使用空值/默认值填充
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public void Linq105() { string [] categories = new string []{ "Beverages" , "Condiments" , "Vegetables" , "Dairy Products" , "Seafood" }; List<Product> products = GetProductList(); var q = from c in categories join p in products on c equals p.Category into ps from p in ps.DefaultIfEmpty() select new { Category = c, ProductName = p == null ? "(No products)" : p.ProductName }; foreach ( var v in q) { Console.WriteLine(v.ProductName + ": " + v.Category); } } |
1 2 3 4 5 6 | Result: Chai: Beverages Aniseed Syrup: Condiments (No products): Vegetables Queso Cabrales: Dairy Products Konbu: Seafood |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】