当前位置:网站首页>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; }
}
}
边栏推荐
猜你喜欢
随机推荐
绘制pdf表格 (一) 通过itext实现在pdf中绘制excel表格样式并且实现下载(支持中文字体)
Starting from business needs, open the road of efficient IDC operation and maintenance
[Hardware Engineer] can't select components?
Brief introduction to clustered index, secondary index, index push down
03. Longest substring without repeated characters
mysql case when
window10系统下nvm的安装步骤以及使用方法
栈的顺序存储结构,链式存储结构及实现
Methods of de duplication and connection query in MySQL database
02. Add two numbers
Interface automation test postman+newman+jenkins
食品安全 | 八问八答带你重新认识小龙虾!这样吃才对!
哈夫曼树的构建
"Digital security" alert NFT's seven Scams
Trooper
PostgreSQL passwords are case sensitive. Is there parameter control?
[Hardware Engineer] Why do DC-DC isolated switching power modules use transformers?
Boring post roast about work and life
[solution] the Microsoft edge browser has the problem of "unable to access this page"
软件测试基础知识(思维导图)








