欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

初学LINQ语句

程序员文章站 2022-07-04 08:43:54
...
//有两个数组,客户和地址,他们之间通过公司名关联:
 var customers = new[] { 
      new {CustomerID=1,FirstName="Kim",LastName="Abercrombie",CompanyName="Alpine Sky House"},
      new {CustomerID=2,FirstName="Jeff",LastName="Hay",CompanyName="Coho Winery"},
      new {CustomerID=3,FirstName="Charlie",LastName="Herb",CompanyName="Alpine Sky House"},
      new {CustomerID=4,FirstName="Chris",LastName="Preston",CompanyName="Trey Reseach"},
      new {CustomerID=5,FirstName="Dave",LastName="Barnett",CompanyName="Wingtip Toys"},
      new {CustomerID=6,FirstName="Ann",LastName="Beebe",CompanyName="Coho Winery"},
      new {CustomerID=7,FirstName="John",LastName="Kane",CompanyName="Wingtip Toys"},
      new {CustomerID=8,FirstName="David",LastName="Simpson",CompanyName="Trey Reseach"},
      new {CustomerID=9,FirstName="Greg",LastName="Abercrombie",CompanyName="Wingtip Toys"},
      new {CustomerID=10,FirstName="Tim",LastName="Abercrombie",CompanyName="Wide World Importers"}
  };

  var addresses = new[] { 
      new {CompanyName="Alpine Sky House",City="Berne",Country="Switzerland"},
      new {CompanyName="Coho Winery",City="San Francisco",Country="United States"},
      new {CompanyName="Trey Reseach",City="New York",Country="United States"},
      new {CompanyName="Wingtip Toys",City="London",Country="United Kingdom"},
      new {CompanyName="Wide World Importers",City="Tetbury",Country="United Kingdom"}
  };

  //------------------筛选出customers数组中客户名(从枚举集合投射指定字段)----------------------
  //1.使用Select方法,用Lambda表达式标识要投射的字段(IEnumerable类可换var)
      IEnumerable<string> customerFirstNames = customers.Select(cust=>cust.FirstName);
  //2.使用from和select查询操作符
      var customerFirstNames = from custs in customers select custs.FirstName;

  //-----------------筛选出地址数组中国家是美国的公司名称(筛选来自可枚举集合的行)----------------------
  //1.使用Where方法,用Lambda表达式指定行的匹配条件
      var usCompanies=addresses.Where(addr=>String.Equals(addr.Country,"United States")).Select(usComp=>usComp.CompanyName);
  //2.使用where操作符
      var usCompanies=from addr in addresses where String.Equals(addr.Country,"United States") select addr.CompamyName;

  //----------------筛选出地址数组中公司名称并降序排序(按特定顺序枚举数据)--------------------------
  //1.使用OrderBy方法,用Lambda表达式标识用于对行进行排序的字段
      var companyNames=addresses.OrderBy(addr=>addr.CompanyName).Select(comp=>comp.CompanyName);
  //2.使用orderby查询操作符
      var companyNames=from addr in addresses orderby addr,CompanyName select addr.CompanyName; 

  //--------------用国家对地址数组进行分组筛选(根据字段的值对数据进行分组)----------------------------
  //1.使用GroupBy方法,用Lambda表达式标识用于对行进行分组的字段
      var companiesGroupedByCountry=addresses.GroupBy(addrs=>Country);
  //2.使用group by查询操作符
      var companiesGroupedByCountry=from addr in addresses group addr by addr.Country; 

  //--------------------关联两个数组查询(联接两个不同集合中的数据)-------------------------------
  //1.使用Join方法指定连接的集合、联接条件和结果字段
      var citiesAndCustomers=Customers.Select(c=>new{c.FirstName,c.LastName,c.CompanyName}).Join(addresses,custs=>custs.CompanyName,addrs=>addrs.CompanyName,(custs,addrs)=>new{custs.FirstName,custs.LastName,addrs.Country});
  //2.使用Join查询操作符
      var citiesAndCustomers=from a in addresses join c in customers on a.CompanyName equals c.CompanyName select new{c.FirstName,c.LastName,a.Country};