All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
Asp.Net,构架
精通数据库系列之入门-技巧篇3_mssql学习_编程技术
跟我学SQL:(九)datetime和interval数据类型_mssql学习_编程技术
ASP.NET2.0缓存(Cache)技术_[Asp.Net教程]
C#,3.0新特征之创建和初始化集合对象_[Asp.Net教程]
最简单快速的Apache二级域名实现方法介绍_[PHP教程]
delphi设置提示信息
C#中类的继承机制_[Asp.Net教程]
.NET,3.x新特性之自动属性及集合初始化_.net资料_编程技术
apache的几个设置(目录,权限等)_php资料_编程技术
Reading number is top 10 pictures
美丽的桂林风光2
Sora aoi possession of boudoir1
Magnificent cloud2
再发一张清纯美眉的照片
Beauty ZhiHuiLin2
海底兵马俑
Wild animals melee moment of life and death1
The real super beauty7
大年初五接财神
A man's favorite things3--ZhouWeiTong
Download software ranking
Boxer's Top ten classic battle6
金山office2007
The Bermuda triangle3
终极变速大师Speeder3.26
Sora aoi, the maid, students' uniforms
Tram sex maniac 2 (H) rar bag1
软件工程思想
Unix video tutorial9
Unix video tutorial15
DreamWeaver8
归海一刀 published in(发表于) 2014/1/30 1:03:22 Edit(编辑)
LINQ体验(7)——LINQ,to,SQL语句之Group,ByHaving和Exists_[Asp.Net教程]

LINQ体验(7)——LINQ,to,SQL语句之Group,ByHaving和Exists_[Asp.Net教程]

LINQ体验(7)——LINQ to SQL语句之Group By/Having和Exists/_[Asp.Net教程]

LINQ体验(7)——LINQ to SQL语句之Group By/Having和Exists/In/Any/All/Contains


我们继续讲解LINQ语句,我昨天发现了LINQ in Action 电子书,就共享了LINQ in Action 电子书这本书。大家也可以看看。这篇我们来讨论Group By/Having操作符和Exists/In/Any/All/Contains操作符。


Group By/Having操作符


适用场景:分组数据,为我们查找数据缩小范围。


说明:分配并返回对传入参数进行分组操作后的可枚举对象。分组;延迟


1.简单形式:

var q =
from p in db.Products
group p by p.CategoryID into g
select g;

说明:from p in db.Products 表示从表中将产品对象取出来。group p by p.CategoryID into g表示对p按CategoryID字段归类。其结果命名为g,一旦重新命名,p的作用域就结束了,所以,最后select时,只能select g。当然,也不必重新命名可以这样写:

var q =
from p in db.Products
group p by p.CategoryID;

我们用示意图表示:

GroupBy分组统计示意图

如果想遍历某类别中所有记录,这样:

foreach (var gp in q)
{
if (gp.Key == 2)
{
foreach (var item in gp)
{
//do something
}
}
}

2.Select匿名类:

var q =
from p in db.Products
group p by p.CategoryID into g
select new { CategoryID = g.Key, g };

说明:在这句LINQ语句中,有2个property:CategoryID和g。这个匿名类,其实质是对返回结果集重新进行了包装。把g的property封装成一个完整的分组。如下图所示:

GroupBy分组匿名类示意图

如果想遍历某匿名类中所有记录,要这么做:

foreach (var gp in q)
{
if (gp.CategoryID == 2)
{
foreach (var item in gp)
{
//do something
}
}
}

3.最大值


说明:先按CategoryID归类,判断各个分类产品中单价最大的Products。取出CategoryID值,并把UnitPrice值赋给MaxPrice。

var q =
from p in db.Products
group p by p.CategoryID into g
select new {
g.Key,
MaxPrice = g.Max(p => p.UnitPrice)
};

4.最小值


说明:先按CategoryID归类,判断各个分类产品中单价最小的Products。取出CategoryID值,并把UnitPrice值赋给MinPrice。

var q =
from p in db.Products
group p by p.CategoryID into g
select new {
g.Key,
MinPrice = g.Min(p => p.UnitPrice)
};

5.平均值


说明:先按CategoryID归类,取出CategoryID值和各个分类产品中单价的平均值。

var q =
from p in db.Products
group p by p.CategoryID into g
select new {
g.Key,
AveragePrice = g.Average(p => p.UnitPrice)
};

6.求和


说明:先按CategoryID归类,取出CategoryID值和各个分类产品中单价的总和。

var q =
from p in db.Products
group p by p.CategoryID into g
select new {
g.Key,
TotalPrice = g.Sum(p => p.UnitPrice)
};

7.计数


说明:先按CategoryID归类,取出CategoryID值和各个分类产品的数量。

var q =
from p in db.Products
group p by p.CategoryID into g
select new {
g.Key,
NumProducts = g.Count()
};

8.带条件计数


说明:先按CategoryID归类,取出CategoryID值和各个分类产品的断货数量。 Count函数里,使用了Lambda表达式,Lambda表达式中的p,代表这个组里的一个元素或对象,即某一个产品。

var q =
from p in db.Products
group p by p.CategoryID into g
select new {
g.Key,
NumProducts = g.Count(p => p.Discontinued)
};

9.Where限制


说明:在翻译成SQL语句时,在最外层嵌套了Where条件。

var q =
from p in db.Products
group p by p.CategoryID into g
where g.Count() >= 10
select new {
g.Key,
ProductCount = g.Count()
};

语句描述:根据产品的―ID分组,查询产品数量大于10的ID和产品数量。


10.Multiple Columns


说明:既按产品的分类,又按供应商分类。在by后面,new出来一个匿名类。这里,Key其实质是一个类的对象,Key包含两个Property:CategoryID、SupplierID。用g.Key.CategoryID可以遍历CategoryID的值。

var categories =
from p in db.Products
group p by new { p.CategoryID, p.SupplierID } into g
select new {g.Key, g};

11.Expression(表达式)


说明:按产品单价是否大于10分类。其结果分为两类,大于的是一类,小于及等于为另一类。

var categories =
from p in db.Products
group p by new { Criterion = p.UnitPrice > 10 } into g
select g;

Exists/In/Any/All/Contains操作符


适用场景:用于判断集合中元素,进一步缩小范围。


Any


说明:用于判断集合中是否有元素满足某一条件;不延迟。(若条件为空,则集合只要不为空就返回True,否则为False)。有2种形式,分别为简单形式和带条件形式。


1.简单形式:

var q =
from c in db.Customers
where !c.Orders.Any()
select c;

生成SQL语句为:
SELECT [t0].[CustomerID], [t0].[CompanyName], [t0].[ContactName], [t0].[ContactTitle], [t0].[Address], [t0].[City], [t0].[Region], [t0].[PostalCode], [t0].[Country], [t0].[Phone], [t0].[Fax]
FROM [dbo].[Customers] AS [t0]
WHERE NOT (EXISTS(
SELECT NULL AS [EMPTY]
FROM [dbo].[Orders] AS [t1]
WHERE [t1].[CustomerID] = [t0].[CustomerID]
))


2.带条件形式:

var q =
from c in db.Categories
where c.Products.Any(p => p.Discontinued)
select c;

生成SQL语句为:
SELECT [t0].[CategoryID], [t0].[CategoryName], [t0].[Description], [t0].[Picture]
FROM [dbo].[Categories] AS [t0]
WHERE EXISTS(
SELECT NULL AS [EMPTY]
FROM [dbo].[Products] AS [t1]
WHERE ([t1].[Discontinued] = 1) AND ([t1].[CategoryID] = [t0].[CategoryID])
)


All


说明:用于判断集合中所有元素是否都满足某一条件;不延迟


1.带条件形式

var q =
from c in db.Customers
where c.Orders.All(o => o.ShipCity == c.City)
select c;

Contains


说明:用于判断集合中是否包含有某一元素;不延迟。它是对两个序列进行连接操作的。

string[] customerID_Set = new string[] { "AROUT", "BOLID", "FISSA" };
var q = (
from o in db.Orders
where customerID_Set.Contains(o.CustomerID)
select o).ToList();

语句描述:查找"AROUT", "BOLID" 和 "FISSA" 这三个客户的订单。先定义了一个数组,在LINQ to SQL中使用Contains,数组中包含了所有的CustomerID,即返回结果中,所有的CustomerID都在这个集合内。也就是in。 你也可以把数组的定义放在LINQ to SQL语句里。比如:

var q = (
from o in db.Orders
where (new string[] { "AROUT", "BOLID", "FISSA" }).Contains(o.CustomerID)
select o).ToList();

Not Contains则取反:

var q = (
from o in db.Orders
where !(new string[] { "AROUT", "BOLID", "FISSA" }).Contains(o.CustomerID)
select o).ToList();

1.包含一个对象:

var order = (from o in db.Orders
where o.OrderID == 10248
select o).First();
var q = db.Customers.Where(p => p.Orders.Contains(order)).ToList();
foreach (var cust in q)
{
foreach (var ord in cust.Orders)
{
//do something
}
}

2.包含多个值:

string[] cities = new string[] { "Seattle", "London", "Vancouver", "Paris" };
var q = db.Customers.Where(p=>cities.Contains(p.City)).ToList();

总结一下这篇我们说明了以下语句:


Group By/Having
分组数据;延迟
Any
用于判断集合中是否有元素满足某一条件;不延迟
All
用于判断集合中所有元素是否都满足某一条件;不延迟
Contains
用于判断集合中是否包含有某一元素;不延迟





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.