LINQ (C#)
背景
一直没搞懂LINQ里面的from、where、select那些,感觉像是SQL查询语句
学习过程
示例代码:

1 class Program 2 { 3 public class Custom 4 { 5 public string City { get; set; } 6 public string FirstName { get; set; } 7 public string LastName { get; set; } 8 public string Address { get; set; } 9 10 } 11 static void Main(string[] args) 12 { 13 List<Custom> Customers = new List<Custom> 14 { 15 new Custom { City = "beijing",FirstName="li",LastName="p",Address="bj" }, 16 new Custom { City = "shanghai", FirstName="sun",LastName="m",Address="sh"}, 17 new Custom { City = "shenzhen",FirstName="wang",LastName="y",Address="sz" } 18 }; 19 var result1 = from c in Customers 20 where c.City.StartsWith("s") 21 orderby c.LastName 22 select new { c.FirstName, c.LastName, c.Address }; 23 24 //与上方代码是等效的 25 //var result2 = Customers.Where(c => c.City.StartsWith("s")) 26 // .OrderBy(c => c.LastName) 27 // .Select(c => new { c.FirstName, c.LastName, c.Address }); 28 foreach (var item in result1) 29 { 30 Console.WriteLine(item.Address); 31 } 32 //output 33 //sh 34 //sz 35 Console.ReadKey(); 36 } 37 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程