背景
一直没搞懂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 }