LINQ补充:
强制立即执行:
执行聚合函数(Count,Max,Average,First)
调用ToList(TSource>)或ToArrat(TSource>)方法缓存结果
使用let扩展范围变量:(数据源为一个字符串数组)
var earlyBirdQuery =
from sentence in strings
let words = sentence.Split(' ')
from word in words
let w = word.ToLower()
where w[0]=='a||w[0]=='e'||
w[0]==t
select word;
let words = sentence.Split(' ') 创建一个可以查询自身的可枚举类型
let w = word.ToLower() 使查询只能对范围变量word调用一次ToLower.如果
不使用let,则必须在where子句的每个布尔条件中调用ToLwer()
筛选:
where cust.City=="London"&&cust.Name=="Devon"
where cust.City=="London"||cust.City=="paris"
使用C#逻辑AND和OR运算符来根据需要在Where子句中应用任意数量的筛选表达
式.
除了不能是第一个或最后一个子句外,它几乎可以放在查询表达式中的任何位置.
排序:
在查询表达式中,orderby子句可以使返回的序列或子序列按升序或降序排序
可以指定多个键,以便执行一个或多个次要排序操作
默认排序顺序为升序
编译时,orderby子句被转换为OrderBy方法的调用.orderby子句中的多个键转换为
ThenBy方法调用.
分组:
group子句返回一个IGrouping(TKey,Telement)对象序列
编译时,group子句被转换为对GroupBy方法的调用.
var studentQuery1= from student in students
group student by student.Last[0];
如果想要对每个组执行附加查询操作,则可以使用into上下文关键字执行一个临时
标示符.使用into时,必须继续编写该查询,并最终使用一个select语句或另外一个
group子句结束该查询
var studentQuery2 = from student in students
group student by student.Last[0] into g
orderby g.Key
select g;
联接:
使用join子句可以将 来自不同源序列并且在对象模型中没有直接关系的元素相关联
唯一的要求是每个源中的元素需要共享某个可以进行比较判断是否相等的值
join子句使用特殊的equals关键字比较指定的键是否相等(只支持equals进行判断)
三种常见的联接类型
内部联接
分组联接
左外联接
内联:
var innerJoinQuery = from category in categores
join prod in products on category.ID equals
prod.CategoryID select new
{Productname==prod.Name,Category=category.name};
分组:含有into 表达式的join子句称为分组联接
var innerJoinQuery = from category in categores
join prod in products on category.ID equals
prod.CategoryID into prodGroup select new
{CategoryName=category.Name, products=prodGroup};
左外:
在左外联接中,将返回左侧源序列中的所有元素,即使它们在右侧序列中没有匹配的元
素也是如此.
若要在LINQ中执行左外联接,请将DefaultIfEmpty方法与分组联接结合起来,以指定
要在某个左侧元素不具有匹配元素时产生的默认右侧元素.可以使用 null作为任何引
用类型的默认值,也可以指定用户定义的默认类型
var leftOuterJoinQuery= from category in categories
join prod in products on category.ID equals
prod.CategoryID into prodGroup
from item in prodGroup.DefaultIfEmpty(new
Product{Name=String.Empty,CategoryID=0}) select new
{CateName=category.Name,ProdName=item.Name};
join选择(投影):
select 子句可以指定将在执行查询时产生的值的类型.该子句的结果将基于前面所有
子句的计算结果以及select子句本身中的所有表达式.
查询表达式必须以select子句group子句结束
在最简单的情况下,select子句仅指定范围变量.这会使返回的序列包含与数据源具有
相同类型的元素.
强制立即执行:
执行聚合函数(Count,Max,Average,First)
调用ToList(TSource>)或ToArrat(TSource>)方法缓存结果
使用let扩展范围变量:(数据源为一个字符串数组)
var earlyBirdQuery =
from sentence in strings
let words = sentence.Split(' ')
from word in words
let w = word.ToLower()
where w[0]=='a||w[0]=='e'||
w[0]==t
select word;
let words = sentence.Split(' ') 创建一个可以查询自身的可枚举类型
let w = word.ToLower() 使查询只能对范围变量word调用一次ToLower.如果
不使用let,则必须在where子句的每个布尔条件中调用ToLwer()
筛选:
where cust.City=="London"&&cust.Name=="Devon"
where cust.City=="London"||cust.City=="paris"
使用C#逻辑AND和OR运算符来根据需要在Where子句中应用任意数量的筛选表达
式.
除了不能是第一个或最后一个子句外,它几乎可以放在查询表达式中的任何位置.
排序:
在查询表达式中,orderby子句可以使返回的序列或子序列按升序或降序排序
可以指定多个键,以便执行一个或多个次要排序操作
默认排序顺序为升序
编译时,orderby子句被转换为OrderBy方法的调用.orderby子句中的多个键转换为
ThenBy方法调用.
分组:
group子句返回一个IGrouping(TKey,Telement)对象序列
编译时,group子句被转换为对GroupBy方法的调用.
var studentQuery1= from student in students
group student by student.Last[0];
如果想要对每个组执行附加查询操作,则可以使用into上下文关键字执行一个临时
标示符.使用into时,必须继续编写该查询,并最终使用一个select语句或另外一个
group子句结束该查询
var studentQuery2 = from student in students
group student by student.Last[0] into g
orderby g.Key
select g;
联接:
使用join子句可以将 来自不同源序列并且在对象模型中没有直接关系的元素相关联
唯一的要求是每个源中的元素需要共享某个可以进行比较判断是否相等的值
join子句使用特殊的equals关键字比较指定的键是否相等(只支持equals进行判断)
三种常见的联接类型
内部联接
分组联接
左外联接
内联:
var innerJoinQuery = from category in categores
join prod in products on category.ID equals
prod.CategoryID select new
{Productname==prod.Name,Category=category.name};
分组:含有into 表达式的join子句称为分组联接
var innerJoinQuery = from category in categores
join prod in products on category.ID equals
prod.CategoryID into prodGroup select new
{CategoryName=category.Name, products=prodGroup};
左外:
在左外联接中,将返回左侧源序列中的所有元素,即使它们在右侧序列中没有匹配的元
素也是如此.
若要在LINQ中执行左外联接,请将DefaultIfEmpty方法与分组联接结合起来,以指定
要在某个左侧元素不具有匹配元素时产生的默认右侧元素.可以使用 null作为任何引
用类型的默认值,也可以指定用户定义的默认类型
var leftOuterJoinQuery= from category in categories
join prod in products on category.ID equals
prod.CategoryID into prodGroup
from item in prodGroup.DefaultIfEmpty(new
Product{Name=String.Empty,CategoryID=0}) select new
{CateName=category.Name,ProdName=item.Name};
join选择(投影):
select 子句可以指定将在执行查询时产生的值的类型.该子句的结果将基于前面所有
子句的计算结果以及select子句本身中的所有表达式.
查询表达式必须以select子句group子句结束
在最简单的情况下,select子句仅指定范围变量.这会使返回的序列包含与数据源具有
相同类型的元素.
浙公网安备 33010602011771号