首页 > 其他 > 详细

LINQ学习笔记(三)

时间:2017-04-09 14:47:26      阅读:198      评论:0      收藏:0      [点我收藏+]

下面对各子句解释

from子句:查询表达式的开始子句,查询表达式必须以from子句开头。

格式:from u in source

其中u表示范围变量,它表示源序列中的每个后续元素,source为数据源。

查询表达式可以包含多个from子句,当源序列中的每个元素本身就集合或者包含集合时,

可使用附加的from子句。

IEnumerable<City> cityQuery =
from country in countries
from city in country.Cities
where city.Population > 10000
select city;

 

group子句:查询语句的一种结束子句

格式 group item by item.Id

其中item是需要分组的源,by后面表达式是分组依据。

使用group子句可以产生按照指定键组织的组序列。键可以为任何数据类型。

 

select子句:查询语句的另一种结束子句

格式 :select item或select new {id=Item.Id,Name=Item.Name}
select和SQL的select一致,表示需要"选择的数据或列"。它可以产生所有其他类型的序列。

简单的select子句只是与数据源中包含对象相同的数据类型(前者)。

使用select子句可以将源数据转换为新类型是序列,这个转换也叫投影(后者)。

var queryNameAndPop =
from country in countries
select new { Name = country.Name, Pop = country.Population };

用new来生成一个匿名类。

 

into子句

可以在select和group之间使用into子句存储查询时的临时标识符,相当于临时变量,用来当做中转站。

当必须在分组或选择操作之后对查询执行附加操作时需要这样做。

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;

把group分组之后的序列通过into存在countryGroup中,在通过where筛选,

orderby排序,最后通过select选中需要的序列或列集合。

 

where子句

格式:where 条件

使用where子句可以根据一个或多个条件筛选数据源的某些元素。

IEnumerable<City> queryCityPop =
from city in cities
where city.Population < 200000 && city.Population > 100000
select city;

当有多个条件时,可以使用&&这样的操作符,也可以使用多个where子句来实现。多个where表示与的关系。

 

orderby子句

格式:orderby  item descending/ascending

使用orderby子句可以按升序或降序对结果进行排序,还可以指定次要排序顺序。

其中desecnding表示降序,ascending表示升序。

IEnumerable<Country> querySortedCountries =
from country in countries
orderby country.Area, country.Population descending
select country;

多个item重要层次一次递减,也就是首先以第一位的依据排序。

 

join子句

join相当于SQL的join连接查询。

使用join子句可以根据每个元素指定键之间的相等比较,对一个数据源中的元素

与另外一个数据源中元素进行关联或组合。在LINQ中,联接操作是针对其元素具

有不同类型的对象序列执行的。在联接两个序列后,不许使用select或group指定

要存储到输出系列中的元素。

var categoryQuery =
from cat in categories
join prod in products on cat equals prod.Category
select new { Category = cat, Name = prod.Name };

on后面接的表达式表示联接筛选条件。select可以筛选cat和prod中的任何成员。

equals表示相等,equals两边的数据类型要求一致。

 

let子句

使用let可以将表达式(如方法的调用)的结果存储到新的范围变量中。

例如:

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 + " ");

一个查询表达式里的范围变量可以同时存在,在select后可同时获取范围变量。

 

let和into是有相似之处的,只是在特殊的情况下必须使用into。

string[] str = { "12","sdfs"};

var temp = from u in str
let a = u
where a=="1"
select new
{
t1 = a,
t2 = u[0]

};

 

 

子查询

查询子句本身可能包含要给查询表达式,该查询表达式成为"子查询",

每个子查询都以它自己的from子句开头,该子句不一定指向第一个from

子句中的同一数据源。

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()
};

 

 

处理Null值

采用预防方式避免null引用异常

var query1 =
from c in categories
where c != null
join p in products on c.ID equals
(p == null ? null : p.CategoryID)
select new { Category = c.Name, Name = p.Name };

因为==操作符的优先级高于?:操作符,

所以上述表达式表示的是p如果等于null则赋值为null。

LINQ学习笔记(三)

原文:http://www.cnblogs.com/xiaoai123/p/6684568.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!