当前位置:网站首页>C# Linq 去重&去重求和
C# Linq 去重&去重求和
2022-07-25 17:47:00 【时光不负所望】
Linq 去重操作三种方式
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
List<_model> _list = new List<_model>();
_list.Add(new _model() {
_sum = 1, _id = 1, _int = 1 });
_list.Add(new _model() {
_sum = 3, _id = 1, _int = 2 });
_list.Add(new _model() {
_sum = 7, _id = 2, _int = 1 });
_list.Add(new _model() {
_sum = 2, _id = 3, _int = 1 });
_list.Add(new _model() {
_sum = 4, _id = 3, _int = 2 });
_list.Add(new _model() {
_sum = 9, _id = 3, _int = 3 });
//返回列表
var _list1 = _list.GroupBy(i => i._id).Select(i => i.OrderByDescending(i => i._int).First()).ToList();
foreach (var item in _list1)
{
Console.WriteLine(item._sum);
Console.WriteLine(item._id);
Console.WriteLine(item._int);
}
//返回求和(单个条件排序)
var _list2 = _list.GroupBy(i => i._id).Select(i => new {
i.Key, _sum = i.Sum(o=>o._sum) }).ToList();
foreach (var item in _list2)
{
Console.WriteLine(item.Key);
Console.WriteLine(item._sum);
}
//返回求和(多个条件排序)
var _list3 = _list.GroupBy(i => new {
i._id,i._int}).Select(i => new {
i.Key, _sum = i.Sum(o => o._sum) }).ToList();
foreach (var item in _list3)
{
Console.WriteLine(item.Key._id);
Console.WriteLine(item.Key._int);
Console.WriteLine(item._sum);
}
//===========================================================================
Console.ReadKey();
}
public class _model
{
public int _sum {
get; set; }
public int _id {
get; set; }
public int _int {
get; set; }
}
}
边栏推荐
- Redis源码与设计剖析 -- 16.AOF持久化机制
- How to fix the first row title when scrolling down in Excel table / WPS table?
- Cross validation (CV) learning notes
- RestTemplate通过泛型实现POST、PUT、DELETE、GET、集合请求以及文件上传(可批量文件、可带参数)的统一封装(可打印日志)
- 简述Synchronized以及锁升级
- 如何看一本书
- I'm also drunk. Eureka delayed registration and this pit!
- Ultimate doll 2.0 | cloud native delivery package
- Automated test Po design model
- Thesis reading_ Multi task learning_ MMoE
猜你喜欢

Highlights

window10系统下nvm的安装步骤以及使用方法

交友活动记录

对灰度图像的三维函数显示

With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development

OSPF综合实验

吴恩达机器学习编程作业无法暂停pause问题解决
P2P 之 UDP穿透NAT的原理与实现

面试官:说说 log.Fatal 和 panic 的区别

RedisTemplate解决高并发下秒杀系统库存超卖方案 — Redis事务+乐观锁机制
随机推荐
面试官:说说 log.Fatal 和 panic 的区别
咨询下flink sql-client怎么处理DDL后,fink sql里面映射表加字段以及JOb?
WPF implements user avatar selector
Notes on Flickr's dataset
2022/7/23
go接口变量的类型断言
UFT(QTP)-总结点与自动化测试框架
Step by step introduction of sqlsugar based development framework (13) -- package the upload component based on elementplus, which is convenient for the project
Summary of knowledge points for final review of server-side architecture design
[cadence Allegro PCB design] permanently modify the shortcut key (customized) ~ it is valid for personal test~
「数字安全」警惕 NFT的七大骗局
Principle and implementation of UDP penetration NAT in P2P
STM32 PAJ7620U2手势识别模块(IIC通信)程序源码详解
函数名指针和函数指针
栈的顺序存储结构,链式存储结构及实现
我想理财,不懂,有没有保本金的理财产品?
stm32F407------SPI
【硬件工程师】关于信号电平驱动能力
Nineteen year old summary
Installation steps and usage of NVM under windows10 system