zoukankan      html  css  js  c++  java
  • 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#)

  • 相关阅读:
    C 标准库
    C 标准库
    C 标准库
    C 标准库
    Chrome内核保存为mhtml(单网页)
    C 标准库
    C 标准库
    【转】AOP
    [转]控制反转与依赖注入模式
    [转]乐观锁、悲观锁、死锁的区别
  • 原文地址:https://www.cnblogs.com/youzi-xuchongyou/p/14133627.html
Copyright © 2011-2022 走看看