linq操作符:分组操作符
分组是根据一个特定的值将序列中的元素进行分组。LINQ只包含一个分组操作符:GroupBy。GroupBy操作符类似于T-SQL语言中的Group By语句。来看看GroupBy的方法定义:
public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);
public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector,
IEqualityComparer<TKey> comparer);
从方法定义中可以看出:GroupBy的返回值类型是:IEnumerable<IGrouping<TKey, TSource>>。其元素类型是IGrouping<TKey, TSource>。TKey属性代表了分组时使用的关键值,TSource属性代表了分组之后的元素集合。遍历IGrouping<TKey, TSource>元素可以读取到每一个TSource类型。看下面的例子:
1、定义Product类,其定义如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace GroupOperation
{
public class Product
{
public int Id { get; set; }
public int CategoryId { get; set; }
public string Name { get; set; }
public double Price { get; set; }
public DateTime CreateTime { get; set; }
}
}
2、在Main()方法中调用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace GroupOperation
{
class Program
{
static void Main(string[] args)
{
List<Product> listProduct = new List<Product>()
{
new Product(){Id=,CategoryId=, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
new Product(){Id=,CategoryId=, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-)},
new Product(){Id=,CategoryId=, Name="活着", Price=,CreateTime=DateTime.Now.AddMonths(-)},
new Product(){Id=,CategoryId=, Name="高等数学", Price=,CreateTime=DateTime.Now.AddMonths(-)},
new Product(){Id=,CategoryId=, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-)}
}; // 查询表达式
var listExpress = from p in listProduct group p by p.CategoryId;
Console.WriteLine("输出查询表达式结果");
foreach (var item in listExpress)
{
Console.WriteLine($"CategoryId:{item.Key}");
foreach(var p in item)
{
Console.WriteLine($"ProduceName:{p.Name},ProductPrice:{p.Price},PublishTime:{p.CreateTime}");
}
}
Console.WriteLine("***************************************");
// 查询方法
var listFun = listProduct.GroupBy(p => p.CategoryId);
Console.WriteLine("输出方法语法结果");
foreach (var item in listFun)
{
Console.WriteLine($"CategoryId:{item.Key}");
foreach (var p in item)
{
Console.WriteLine($"ProduceName:{p.Name},ProductPrice:{p.Price},PublishTime:{p.CreateTime}");
}
}
Console.ReadKey();
}
}
}
结果:

下面在来看看多个分组条件的例子。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace GroupOperation
{
class Program
{
static void Main(string[] args)
{
List<Product> listProduct = new List<Product>()
{
new Product(){Id=,CategoryId=, Name="C#高级编程第10版", Price=100.67,CreateTime=DateTime.Now},
new Product(){Id=,CategoryId=, Name="Redis开发和运维", Price=69.9,CreateTime=DateTime.Now.AddDays(-)},
new Product(){Id=,CategoryId=, Name="活着", Price=,CreateTime=DateTime.Now.AddMonths(-)},
new Product(){Id=,CategoryId=, Name="高等数学", Price=,CreateTime=DateTime.Now.AddMonths(-)},
new Product(){Id=,CategoryId=, Name="国家宝藏", Price=52.8,CreateTime=DateTime.Now.AddMonths(-)}
}; // 查询表达式
var list = from p in listProduct group p by new { p.CategoryId, p.Price };
Console.WriteLine("查询表达式方式1输出:");
foreach (var item in list)
{
Console.WriteLine("key:" + item.Key);
foreach (var subItem in item)
{
Console.WriteLine($"ProduceName:{subItem.Name},ProductPrice:{subItem.Price},PublishTime:{subItem.CreateTime}");
}
}
var listExpress = from p in listProduct
group p by new { p.CategoryId, p.Price } into r // 使用into把数据填充到局部变量r中,然后select筛选数据
select new { key = r.Key, ListGroup = r.ToList() };
Console.WriteLine("查询表达式方式2输出:");
foreach(var item in listExpress)
{
Console.WriteLine("key:"+item.key);
foreach (var subItem in item.ListGroup)
{
Console.WriteLine($"ProduceName:{subItem.Name},ProductPrice:{subItem.Price},PublishTime:{subItem.CreateTime}");
}
} // 方法语法
var listFun = listProduct.GroupBy(p => new { p.CategoryId, p.Price }).Select(g => new { key = g.Key, ListGroup = g.ToList() });
Console.WriteLine("方法语法输出:");
foreach (var item in listFun)
{
Console.WriteLine("key:" + item.key);
foreach (var subItem in item.ListGroup)
{
Console.WriteLine($"ProduceName:{subItem.Name},ProductPrice:{subItem.Price},PublishTime:{subItem.CreateTime}");
}
} Console.ReadKey();
}
}
}
结果:

linq操作符:分组操作符的更多相关文章
- LINQ系列:Linq to Object分组操作符
分组是指根据一个特定的值将序列中的值或元素进行分组.LINQ只包含一个分组操作符:GroupBy. GroupBy 1>. 原型定义 public static IQueryable<IG ...
- LINQ标准查询操作符详解(转)
一. 关于LINQ LINQ 英文全称是“Language-Integrated Query”,中文为“语言集成查询”,它是微软首席架构师.Delphi 之父和C# 之父——Anders ...
- LINQ标准查询操作符(二)——Join、GroupJoin、GroupBy、Concat、
四.联接操作符 联接是指将一个数据源对象与另一个数据源对象进行关联或者联合的操作.这两个数据源对象通过一个共同的值或者属性进行关联. LINQ有两个联接操作符:Join和GroupJoin. 1. J ...
- Linq 标准查询操作符三
本文介绍了LINQ标准查询操作符.没有这些操作符,LINQ就不会存在.本文为理解这些操作符的功能提供了很好的基础.了解它们将会很有帮助,因为LINQ的各种Provider都是基于这些操作符来完成各自丰 ...
- Linq的查询操作符
Linq有表达式语法和调用方法的语法.两者是可以结合使用,通常情况下也都是结合使用.表达式语法看上去比较清晰而调用方法的语法实现的功能更多,在此文章中介绍的是表达式语法.方法语法可以看System.L ...
- Linq标准查询操作符
Linq的出现让代码简洁了不少.之前在项目中基本都在使用它,但是没有完整的整理过,今天借这个周末,将其进行整理,方便后期对其的使用.Linq的操作可以分为聚合,连接,转换,元素操作符,相等操作,生成 ...
- LINQ 标准查询操作符
本文介绍了LINQ标准查询操作符.没有这些操作符,LINQ就不会存在.本文为理解这些操作符的功能提供了很好的基础.了解它们将会很有帮助,因为LINQ的各种Provider都是基于这些操作符来完成各自丰 ...
- Linq学习之操作符
一.环境搭建 下面将逐步搭建我们学习的环境,这个环境不仅仅是这次需要使用,以后的教程一样需要使用这个环境.所以请大家务必按照 搭建这里的环境否则会影响你后面的学习. 我们用到的几张表 通知消息表: 用 ...
- LINQ标准查询操作符(三)——Aggregate、Average、Distinct、Except、Intersect、Union、Empty、DefaultIfEmpty、Range、Repeat
七.聚合操作符 聚合函数将在序列上执行特定的计算,并返回单个值,如计算给定序列平均值.最大值等.共有7种LINQ聚合查询操作符:Aggregate.Average.Count.LongCount.Ma ...
随机推荐
- SpringBoot不使用模板引擎直接返回html
一.在resource目录下面建立文件夹,里面方静态页面. 路径:src\main\resources\static\page\index.html 访问:http://localhost:8080/ ...
- android sqlite3:数据库操作
1. 修改表名: alter table notification rename to notification_test 2. 修改数据库名称: 3. 复制一个表的数据到另外一个表中(表的字段一致) ...
- jqgrid 设置为每行单选
jqgrid 不支持单选,自己自带了多选multiselect 那么单选怎么做呢,可以参考如下配置 multiselect: true, multiboxonly:true, gridComplete ...
- 微信支付错误,页面URL末注册
最近在做个项目用到微信支付的JSSDK支付时候碰到“URL末注册的问题”,可是我已经在公众平台里的支付目录里添加了,测试了几次都是这个问题,最后才发现原来是大小写的问题,还有我的支付页面是ASP.NE ...
- SPI和RAM IP核
学习目的: (1) 熟悉SPI接口和它的读写时序: (2) 复习Verilog仿真语句中的$readmemb命令和$display命令: (3) 掌握SPI接口写时序操作的硬件语言描述流程(本例仅以写 ...
- remoting生命周期
https://www.cnblogs.com/luomingui/archive/2011/07/09/2101779.html
- mysql中tinyint、smallint、int、bigint的区别
tinyint 从 -2^7 (-128) 到 2^7 - 1 (123) 的整型数据.存储大小为 1 个字节. unsigned 是从 0 到 255 的整型数据. 所以建表的时候 只能是tinyi ...
- Oracle Time Model Statistics(时间模型统计)
Oracle数据库从10g開始,启用以时间模型统计为主.命中率为辅等性能度量指标.基于时间模型统计,主要是基于操作类型測量在数据库中花费的时间的统计信息.最重要的时间模型统计是数据库时间.或DB时间. ...
- nodejs文件上传报错总结
语法: fs.rename(oldPath,newPath,callback) 今天在使用formidable模块做图片上传处理的时候,fs.rename方法的报了一个这样的错:cross-devic ...
- CNN及其可解释性
https://stats385.github.io/readings https://arxiv.org/pdf/1311.2901.pdf A Mathematical Theory of Dee ...