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     }
View Code
复制代码

参考网址

Understanding LINQ (C#)

posted @   Youse的二分口粮地  阅读(63)  评论(0编辑  收藏  举报
编辑推荐:
· 基于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保姆级教程
点击右上角即可分享
微信分享提示