首页 > Linux操作系统 > Linux操作系统 > GroupBy 操作入门
var q =
from p in db.Products
group p by p.CategoryID into g
select g;在这里,from p in db.Products 表示从表中将对象纪录取出来。p就是每一个产品。而后面一句,group p by p.CategoryID into g表示对p进行归类,按什么归类呢,就是要按我们设定的CategoryID这个字段。而这个归类的结果,被重新命名为g,而且是必须重新命名。一旦重新命名,p的作用域就结束了,所以,最后select时,只能select g。
那有人就要问了,不重新命名可以吗?回答,可以。那你的select语句就没有了,上面语句等同与
var q = from p in db.Products
group p by p.CategoryID;这是query表达式中,很少见的没有select语句的情况。我记忆中,好像只有此一种。我们还是按
var q =
from p in db.Products
group p by p.CategoryID into g
select g;
来讲解。因为这种更好理解。在T-Sql中,groupby只是用来做分组统计,计算平均值,最大值,求和等等,而在dlinq中的groupby,则发挥到了极值。我们用ToList()取到结果,来看看上面语句返回的结果。如图:

Northwind db = new Northwind(ConStr);
db.Log = Console.Out;
Query#region Query
var q = from p in db.Products
group p by p.CategoryID into g
select g;
#endregion Query
Verification#region Verification
foreach (var gp in q)
{
}
return;
#endregion Verification单步调试该foreach段代码,foreach的目的是让dlinq加载数据。在console windows上,你可以看到如下输出。
SELECT [t0].[CategoryID] AS [Key]
FROM [Products] AS [t0]
GROUP BY [t0].[CategoryID]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 2.0.20612.0
SELECT [t0].[ProductID], [t0].[ProductName], [t0].[SupplierID], [t0].[CategoryID
], [t0].[QuantityPerUnit], [t0].[UnitPrice], [t0].[UnitsInStock], [t0].[UnitsOnO
rder], [t0].[ReorderLevel], [t0].[Discontinued]
FROM [Products] AS [t0]
WHERE ((@x1 IS NULL) AND ([t0].[CategoryID] IS NULL)) OR ((@x1 IS NOT NULL) AND
([t0].[CategoryID] IS NOT NULL) AND (@x1 = [t0].[CategoryID]))
-- @x1: Input Int32 (Size = 0; Prec = 0; Scale = 0) [1]
-- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 2.0.20612.0
原来dlinq不仅仅是做了一个groupby操作呀,他还针对每个组,都去做了一个遍历呀。我们可以用下面的图来理解。
外面的大圈,表示全集。而这个全集被分成了若干个小的集合。每个集合就叫做g。在g集合中,包含一个系统定义的Property,叫做Key。key值实际为分类时所用字段的值。而另外一部分,是该类别中所有的product.所以,我们如果想遍历某类别中所有的纪录,要这么来做。
var q = from p in db.Products
group p by p.CategoryID into g
select g; 
foreach (var gp in q)
{
if (gp.Key == 7)
{
foreach (var p in gp)
{
//do something
}
}
}来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12641349/viewspace-144775/ ,如需转载,请注明出处,否则将追究法律责任。