本文来自:http://blog.csdn.net/xuejianwu/article/details/6931804
一、投影操作符
1. Select
Select操作符对单个序列或集合中的值进行投影。下面的示例中使用select从序列中返回Employee表的所有列:
[csharp] view plaincopy
- using (NorthwindDataContext db=new NorthwindDataContext())
- {
- //查询语法
- var query =
- from e in db.Employees
- where e.FirstName.StartsWith("M")
- select e;
- //方法语法
- var q =
- db.Employees
- .Where(e => e.FirstName.StartsWith("M"))
- .Select(e => e);
- foreach (var item in query)
- {
- Console.WriteLine(item.FirstName);
- }
- }
当然,你也可以返回单个列,例如:
[csharp] view plaincopy
- var query =
- from e in db.Employees
- where e.FirstName.StartsWith("M")
- select e.FirstName;
你也可以返回序列中的某几列,例如:
[csharp] view plaincopy
- var query =
- from e in db.Employees
- where e.FirstName.StartsWith("M")
- select new
- {
- e.FirstName,
- e.LastName,
- e.Title
- };
2. SelectMany
SelectMany操作符提供了将多个from子句组合起来的功能,它将每个对象的结果合并成单个序列。下面是一个示例:
[csharp] view plaincopy
- using (NorthwindDataContext db=new NorthwindDataContext())
- {
- //查询语法
- var query =
- from e in db.Employees
- from o in e.Orders
- select o;
- //方法语法
- var q =
- db.Employees
- .SelectMany(e => e.Orders);
- foreach (var item in query)
- {
- Console.WriteLine(item.Freight);
- }
- }
二、限制操作符
Where是限制操作符,它将过滤标准应用在序列上,按照提供的逻辑对序列中的数据进行过滤。
Where操作符不启动查询的执行。当开始对序列进行遍历时查询才开始执行,此时过滤条件将被应用到查询中。Where操作符的使用方法已经在第一节中出现过,这里不再冗述。
三、排序操作符
排序操作符,包括OrderBy、OrderByDescending、ThenBy、ThenByDescending和Reverse,提供了升序或者降序排序。
1. OrderBy
OrderBy操作符将序列中的元素按照升序排列。下面的示例演示了这一点:
[csharp] view plaincopy
- using (NorthwindDataContext db = new NorthwindDataContext())
- {
- //查询语法
- var query =
- from e in db.Employees
- orderby e.FirstName
- select e;
- //方法语法
- var q =
- db.Employees
- .OrderBy(e => e.FirstName)
- .Select(e => e);
- foreach (var item in q)
- {
- Console.WriteLine(item.FirstName);
- }
- }
这里可以使用OrderBy的重载方法OrderBy(Func<T,TKey>,IComparer<Tkey>)来指定序列的排序方式。
2. OrderByDescending
OrderByDescending操作符将序列中的元素按照降序排列。用法与OrderBy相同,这里不再演示。
3. ThenBy
ThenBy操作符实现按照次关键字对序列进行升序排列。此操作符的查询语法与方法语法略有不同,以下代码演示了这一点:
[csharp] view plaincopy
- using (NorthwindDataContext db = new NorthwindDataContext())
- {
- //查询语法
- var query =
- from e in db.Employees
- orderby e.FirstName,e.LastName
- select e;
- //方法语法
- var q =
- db.Employees
- .OrderBy(e => e.FirstName)
- .ThenBy(e => e.LastName)
- .Select(e => e);
- foreach (var item in query)
- {
- Console.WriteLine(item.FirstName);
- }
- }
4. ThenByDescending
ThenByDescending操作符实现按照次关键字对序列进行降序排列。此操作符的查询语法与方法语法略有不同,以下代码演示了这一点:
[csharp] view plaincopy
- using (NorthwindDataContext db = new NorthwindDataContext())
- {
- //查询语法
- var query =
- from e in db.Employees
- orderby e.FirstName,e.LastName descending
- select e;
- //方法语法
- var q =
- db.Employees
- .OrderBy(e => e.FirstName)
- .ThenByDescending(e => e.LastName)
- .Select(e => e);
- foreach (var item in query)
- {
- Console.WriteLine(item.FirstName);
- }
- }
5. Reverse
Reverse将会把序列中的元素按照从后到前的循序反转。需要注意的是,Reverse方法的返回值是void,以下代码演示了这一点:
[csharp] view plaincopy
- using (NorthwindDataContext db = new NorthwindDataContext())
- {
- //方法语法
- var q =
- db.Employees
- .Select(e => e.FirstName)
- .ToList();
- q.Reverse();
- foreach (var item in q)
- {
- Console.WriteLine(item);
- }
- }