查询表达式
应用对象:IEnumerable 或 IQueryable
from子句
查询表达式必须以from开头,效果可以类比foreach
from item in items
where子句
指定将在查询表达式中返回数据源中的哪些元素
// 可以传入数值 也可以 传入函数
where num > 5
where IsEven(num)
select子句
选择筛选的结果
from item in items
select item
select item.name
group子句
分组
group student by student.name
希望按照多个键对元素进行分组时,可使用复合键。 使用匿名类型或命名类型来存储键元素,创建复合键。
group person by new {name = person.surname, city = person.city};
into
可使用 into 上下文关键字创建临时标识符,将 group、join 或 select 子句的结果存储至新标识符
group w by w[0] into fruitGroup
orderby子句
在查询表达式中,orderby 子句可导致返回的序列或子序列(组)以升序或降序排序。
// 可按多个键排序 默认升序
orderby student.Last ascending, student.First ascending
join 联接
join 子句可用于将来自不同源序列并且在对象模型中没有直接关系的元素相关联。
- 内部联接
join prod in products on category.ID equals prod.CategoryID
- 分组联接
join prod in products on category.ID equals prod.CategoryID
- 左外部联接(需要使用DefaultIfEmpty)
join prod in products on category.ID equals prod.CategoryID into prodGroup
from item in prodGroup.DefaultIfEmpty(new Product { Name = String.Empty, CategoryID = 0 })
let子句
用于存储子表达式
let words = sentence.Split(' ')