LINQ包括五个部分:LINQto Objects、LINQ to DataSets、LINQ to SQL、LINQ to Entities、LINQ to XML。数据库
“查询”是指一组指令,这些指令描述要从一个或多个给定数据源检索的数据以及返回的数据应该使用的格式和组织形式。 查询不一样于它所产生的结果。
一般,源数据会在逻辑上组织为相同种类的元素序列。 SQL 数据库表包含一个行序列。 与此相似,ADO.NET DataTable 包含一个 DataRow 对象序列。 在 XML 文件中,有一个 XML 元素“序列”(不过这些元素按分层形式组织为树结构)。 内存中的集合包含一个对象序列。
从应用程序的角度来看,原始源数据的具体类型和结构并不重要。 应用程序始终将源数据视为一个 IEnumerable<T> 或 IQueryable<T> 集合。 在 LINQ to XML 中,源数据显示为一个 IEnumerable<XElement>。 在 LINQ to DataSet 中,它是一个 IEnumerable<DataRow>。 在 LINQ to SQL 中,它是您定义用来表示 SQL 表中数据的任何自定义对象的 IEnumerable 或 IQueryable。数组
查询结果3种:ui
IEnumerable<int> highScoresQuery = from score in scores where score > 80 orderby score descending select score;
IEnumerable<string> highScoresQuery2 = from score in scores where score > 80 orderby score descending select String.Format("The score is {0}", score);
int highScoreCount = (from score in scores where score > 80 select score) .Count(); -or- IEnumerable<int> highScoresQuery3 = from score in scores where score > 80 select score; int scoreCount = highScoresQuery3.Count();
“查询表达式”是用查询语法表示的查询,它像其它表达式同样,能够应用于任何C#表达式有效的上下文中。spa
查询表达式由一组用相似于 SQL 或 XQuery 的声明性语法编写的子句组成。 每一个子句又包含一个或多个 C# 表达式,而这些表达式自己又多是查询表达式或包含查询表达式。code
查询表达式必须以 from 子句开头,而且必须以 select 或 group 子句结尾。 在第一个 from 子句和最后一个 select 或 group 子句之间,查询表达式能够包含一个或多个下列可选子句:where、orderby、join、let 甚至附加的 from 子句。 还可使用 into 关键字使 join 或 group 子句的结果可以充当同一查询表达式中附加查询子句的源。orm
存储查询自己,而非结果的变量,LINQ规范中,变量命名常以query结尾。 对象
查询变量能够显示或隐式声明,使用隐式声明用 var 关键字指示编译器在编译时推断查询变量的类型。blog
static void Main() { // Data source. int[] scores = { 90, 71, 82, 93, 75, 82 }; // Query Expression. IEnumerable<int> scoreQuery = //query variable from score in scores //required where score > 80 // optional orderby score descending // optional select score; //must end with select or group // Execute the query to produce the results foreach (int testScore in scoreQuery) { Console.WriteLine(testScore); } } // Outputs: 93 90 82 82
上例中,查询变量并不存储实际的结果数据(这些数据是在 foreach
循环中产生的),另外,当 foreach
语句执行时,查询结果并非经过查询变量 scoreQuery
返回的。 相反,它们是经过迭代变量 testScore
返回的。 能够在另外一个 foreach
循环中迭代 scoreQuery
变量。 只要该变量和数据源都没有修改,该变量都将产生相同的结果。排序
查询变量能够存储用查询语法或方法语法(或两者的组合)表示的查询,以下:ip
// Query syntax IEnumerable<City> queryMajorCities = from city in cities where city.Population > 100000 select city; // Method-based syntax IEnumerable<City> queryMajorCities2 = cities.Where(c => c.Population > 100000);
查询表达式必须以 from 子句开头。 它同时指定了数据源和范围变量。 在对源序列进行遍历的过程当中,范围变量表示源序列中的每一个后续元素。 将根据数据源中元素的类型对范围变量进行强类型化。 在下面的示例中,由于 countries 是 Country 对象数组,因此范围变量也被类型化为 Country, 这样就可使用点运算符来访问该类型的任何可用成员。
IEnumerable<Country> countryAreaQuery = from country in countries where country.Area > 500000 //sq km select country;
在使用分号或延续子句退出查询以前,范围变量将一直位于范围中。
查询表达式能够包含多个 from 子句。 当源序列中的每一个元素自己就是集合或包含集合时,可以使用附加的 from 子句。 例如,假定您具备一个 Country 对象集合,而其中每一个对象都包含一个名为 Cities 的 City 对象集合。 若要查询每一个 Country 中的 City 对象,请使用两个from 子句,以下所示:
IEnumerable<City> cityQuery = from country in countries from city in country.Cities where city.Population > 10000 select city;
查询表达式必须以 select
子句或 group
子句结尾。
select 子句
使用 select 子句可产生全部其余类型的序列。 简单的 select 子句只是产生与数据源中包含的对象具备相同类型的对象的序列。
在此示例中,数据源包含 Country 对象。 orderby 子句只是将元素从新排序,而 select 子句则产生从新排序的 Country 对象的序列。
IEnumerable<Country> sortedQuery = from country in countries orderby country.Area select country;
可使用 select 子句将源数据转换为新类型的序列。 这一转换也称为“投影”。
// Here var is required because the query // produces an anonymous type. var queryNameAndPop = from country in countries select new { Name = country.Name, Pop = country.Population };
group 子句
使用 group 子句可产生按照指定的键组织的组序列, 键能够采用任何数据类型。 例如,下面的查询建立一个组序列,该序列包含一个或多个 Country 对象,而且它的键是 char 值。
var queryCountryGroups = from country in countries group country by country.Name[0]; //country.name[0] is char type.
使用“into”进行延续
能够在 select 或 group 子句中使用 into 关键字来建立用于存储查询的临时标识符。 当您必须在分组或选择操做以后对查询执行附加查询操做时,须要这样作。
在下面的示例中,以一千万人口范围为界对 countries 进行分组。 在建立这些组以后,使用附加子句筛选掉某些组,而后按升序对剩下的组进行排序。 若要执行这些附加操做,须要使用由 countryGroup 表示的延续。
// percentileQuery is an IEnumerable<IGrouping<int, Country>> var percentileQuery = from country in countries let percentile = (int) country.Population / 10000000 group country by percentile into countryGroup where countryGroup.Key >= 20 orderby countryGroup.Key select countryGroup; // grouping is an IGrouping<int, Country> foreach (var grouping in percentileQuery) { Console.WriteLine(grouping.Key); foreach (var country in grouping) Console.WriteLine(country.Name + ":" + country.Population); }
在 from
开始子句以及 select
或 group
结束子句之间,全部其余子句(where
、join
、orderby
、from
、let
)都是可选的。 任何可选子句均可以在查询正文中使用零次或屡次。
where 子句
使用 where 子句能够根据一个或多个谓词表达式筛选掉源数据中的某些元素。
如下示例中的 where 子句含有两个谓词:
IEnumerable<City> queryCityPop = from city in cities where city.Population < 200000 && city.Population > 100000 select city;
orderby 子句
使用 orderby 子句能够按升序或降序对结果进行排序。 您还能够指定次要排序顺序。
下面的示例使用 Area 属性对 country 对象执行主要排序, 而后使用 Population 属性执行次要排序:
IEnumerable<Country> querySortedCountries = from country in countries orderby country.Area, country.Population descending select country;
join 子句
使用 join 子句能够根据每一个元素中指定键之间的相等比较,对一个数据源中的元素与另一个数据源中的元素进行关联和/或组合。
在 LINQ 中,联接操做是针对其元素具备不一样类型的对象序列执行的。 在联接两个序列以后,必须使用 select 或 group 语句指定要存储到输出序列中的元素。 还可使用匿名类型将每组关联元素中的属性组合为输出序列的新类型。
下面的示例对其 Category 属性与 categories 字符串数组中的某个类别相匹配的 prod 对象进行关联。 其 Category 不与 categories 中的任何字符串匹配的产品会被筛选掉。 select 语句投影了一个新类型,其属性取自 cat 和 prod。
var categoryQuery = from cat in categories join prod in products on cat equals prod.Category select new { Category = cat, Name = prod.Name };
let 子句
使用 let 子句能够将表达式(如方法调用)的结果存储到新的范围变量中。
在下面的示例中,范围变量 firstName 存储了 Split 返回的字符串数组的第一个元素:
string[] names = { "Svetlana Omelchenko", "Claire O'Donnell", "Sven Mortensen", "Cesar Garcia" }; IEnumerable<string> queryFirstNames = from name in names let firstName = name.Split(new char[] { ' ' })[0] select firstName; foreach (string s in queryFirstNames) Console.Write(s + " "); //Output: Svetlana Claire Sven Cesar
查询子句自己可能包含一个查询表达式,该查询表达式有时称为“子查询”。
每一个子查询都以它本身的 from 子句开头,该子句不必定指向第一个 from 子句中的同一数据源。
例如,下面的查询演示了一个在 select 语句中使用的查询表达式,用来检索分组操做的结果。
var queryGroupMax = from student in students group student by student.GradeLevel into studentGroup select new { Level = studentGroup.Key, HighestScore = (from student2 in studentGroup select student2.Scores.Average()) .Max() };
[1] MSDN,LINQ查询表达式
[2] 维基百科,LINQ语言集成查询