Linq基本语法概述

开发 后端
这里介绍Linq基本语法,包括names隐式类型的变量、使用Enumerable.Distinct<T>()、调用Enumberalbe扩展函数和Reverse<T>()等方面。

在向大家详细介绍Linq基本语法之前,首先让大家了解下调用Enumberalbe扩展函数,然后全面介绍Linq基本语法。

Linq基本语法

  1. var result = from item in container orderby value ascending/descending select item; 

1、获取全部记录

  1. var allCars = from c in myCars select c; 

2、只获取字段名称

  1. var names = from c in myCars select c.PetName; 

这里names就是隐式类型的变量。

3、使用Enumerable.Distinct<T>()

  1. var makes = (from c in myCars select c.Make).Distinct<string>(); 

4、即可以在定义的时候调用Enumberalbe扩展函数

  1. var names = from c in myCars select c.PetName;  
  2. foreach (var n in names)  
  3. {  
  4. Console.WriteLine("Name: {0}", n);  

也可以在兼容的数组类型上调用

  1. var makes = from c in myCars select c.Make;  
  2. Console.WriteLine("Distinct makes:");  
  3. foreach (var m in makes.Distinct<string>())  
  4. {  
  5. Console.WriteLine("Make: {0}", m);  

 

  1. // Now get only the BMWs.  
  2. var onlyBMWs = from c in myCars where c.Make == "BMW" select c; 

 

  1. // Get BMWs going at least 100 mph.  
  2. var onlyFastBMWs = from c in myCars  
  3. where c.Make == "BMW" && c.Speed >= 100  
  4. select c; 

5、生成新的数据类型(投影)

  1. var makesColors = from c in myCars select new {c.Make, c.Color}; 

6、Reverse<T>()

或者

  1. var subset = (from c in myCars select c).Reverse<Car>();  
  2. foreach (Car c in subset)  
  3. {  
  4. Console.WriteLine("{0} is going {1} MPH", c.PetName, c.Speed);  

7、排序

默认是ascending

  1. // Order all the cars by PetName.  
  2. var subset = from c in myCars orderby c.PetName select c;  
  3. // Now find the cars that are going less than 55 mph,  
  4. // and order by descending PetName  
  5. subset = from c in myCars  
  6. where c.Speed > 55 orderby c.PetName descending select c; 

默认顺序时也可以明确指明

  1. var subset = from c in myCars  
  2. orderby c.PetName ascending select c; 

8、Enumerable.Except()
两个IEnumerable<T>兼容的对象的差集

  1. static void GetDiff()  
  2. {  
  3. List<string> myCars = new List<String> 
  4. { "Yugo", "Aztec", "BMW"};  
  5. List<string> yourCars = new List<String> 
  6. { "BMW", "Saab", "Aztec" };  
  7. var carDiff =(from c in myCars select c)  
  8. .Except(from c2 in yourCars select c2);  
  9. Console.WriteLine("Here is what you don't have, but I do:");  
  10. foreach (string s in carDiff)  
  11. Console.WriteLine(s); // Prints Yugo.  

以上介绍Linq基本语法

【编辑推荐】

  1. Linq to SQL学习经验
  2. Linq隐式类型化局部变量
  3. Linq匿名类型简单概述
  4. Linq Lambda表达式剖析
  5. Linq对象初始值浅谈
责任编辑:佚名 来源: IT168
相关推荐

2009-09-10 13:42:47

Linq UserIn

2009-09-10 11:10:21

Linq Librar

2009-09-18 16:20:36

LINQ基础

2009-09-08 11:25:42

Linq foreac

2009-09-08 16:08:44

Linq使用order

2009-09-18 16:07:10

Linq Where操

2009-09-10 16:28:17

LINQ查询

2009-09-18 13:44:38

LINQ设计模式

2009-09-14 15:15:45

LINQ技术

2009-09-16 17:21:53

LINQ遍历

2009-09-11 12:13:40

LINQ to SQL

2009-09-09 16:07:16

Linq实体关系

2009-09-09 16:01:21

Linq实体继承使用

2009-09-11 10:16:07

Linq匿名类型

2009-09-18 16:12:22

LINQ TO SQL

2009-09-15 11:29:04

LINQ to SQL

2009-09-15 13:37:24

Linq To Sql

2009-09-17 11:35:40

Linq匿名类型

2009-09-11 10:38:03

LINQ体系结构

2009-09-09 16:53:53

LINQ查询语法
点赞
收藏

51CTO技术栈公众号