使用linq之前获取对象的总和

Get sum of objects before with linq
2021-07-31
  •  译文(汉语)
  •  原文(英语)

我有下面的清单.

列表

  • 3约翰
  • 1斯卡利
  • 4爱德华
  • 9金

尝试使用linq将其转换为;

列表

  • 3约翰
  • 4 Scully
  • 8爱德华
  • 17金

我该怎么办而没有for循环.我想用linq实现.

速聊1:
你能解释一下这个数字是什么意思吗?
速聊2:
您当前的Linq是什么样的?
速聊3:
OMG有史以来最糟糕的解释,那个数字是什么意思!?
速聊4:
列表中是否包含字符串?元组?还有别的吗 您要修改现有列表还是创建新列表?
速聊5:
我认为我得到的是值的总和,因此John = 3 + 1,Scully = 4 +1 + 3,Edward = 9 + 4 +1 + 3
解决过程1

在LINQ中还没有一种方法可以做到这一点.您将不得不编写自己的.尝试这样的事情:

using System;
using System.Collections.Generic;

static class MyExtensions
{
    public static IEnumerable<TResult> AggregatingSelect<TItem, TAggregate, TResult>(
        this IEnumerable<TItem> items,
        Func<TItem, TAggregate, TAggregate> aggregator,
        TAggregate initial,
        Func<TItem, TAggregate, TResult> projection)
    {
        TAggregate aggregate = initial;
        foreach (TItem item in items)
        {
            aggregate = aggregator(item, aggregate);
            yield return projection(item, aggregate);
        }
    }

}
class Program
{
    static void Main(string[] args)
    {
        var numbers = new[] { 3, 1, 4, 9 };
        var result = numbers.AggregatingSelect(
            (int item, int agg) => item + agg, 
            0, 
            (int item, int agg) => agg);
        Console.WriteLine(string.Join(",", result));
    }
}
速聊1:
谢谢你的榜样.
解决过程2

只需使用经典的for循环即可.方式更简单,可理解,可维护且高效.

LINQ基本上不设计为根据给定列表的先前顺序值来更新项目.

速聊1:
尽管由于某些未知的原因,LINQ似乎使一切变得凉爽:)
速聊2:
是的,我不明白"我想用LINQ做到这一点,请不要循环".有趣的是,LINQ总是在内部执行循环.

I have list like below.

List

  • 3 John
  • 1 Scully
  • 4 Edward
  • 9 Kim

Trying to convert this with linq to;

List

  • 3 John
  • 4 Scully
  • 8 Edward
  • 17 Kim

How can i do that without for loop. I want to achieve this with linq.

Talk1:
Could you explain what this numbers means?
Talk2:
what does your current Linq look like?
Talk3:
OMG worst explanation ever, what does that numbers mean!?
Talk4:
Does the list contain strings? Tuples? Something else? Do you want to modify an existing list or create a new one?
Talk5:
I think I get it is the sum of values, so John = 3 + 1, Scully = 4 + 1 + 3, Edward = 9 + 4 + 1 + 3
Solutions1

There isn't a method to do that in LINQ already; you're going to have to write your own. Try something like this:

using System;
using System.Collections.Generic;

static class MyExtensions
{
    public static IEnumerable<TResult> AggregatingSelect<TItem, TAggregate, TResult>(
        this IEnumerable<TItem> items,
        Func<TItem, TAggregate, TAggregate> aggregator,
        TAggregate initial,
        Func<TItem, TAggregate, TResult> projection)
    {
        TAggregate aggregate = initial;
        foreach (TItem item in items)
        {
            aggregate = aggregator(item, aggregate);
            yield return projection(item, aggregate);
        }
    }

}
class Program
{
    static void Main(string[] args)
    {
        var numbers = new[] { 3, 1, 4, 9 };
        var result = numbers.AggregatingSelect(
            (int item, int agg) => item + agg, 
            0, 
            (int item, int agg) => agg);
        Console.WriteLine(string.Join(",", result));
    }
}
Talk1:
Thank you for your example.
Solutions2

Just do it with a classic for loop. Way more simple, understandable, maintainable and efficient.

LINQ is basically not designed to update items depending on previous ordered values of a given list.

Talk1:
Though everything seems to be cooler with LINQ for some unknown reason :)
Talk2:
yes, I don't get the general "I want to do this with LINQ, no loops please". The funny thing is, LINQ always executes loops internally...
转载于:https://stackoverflow.com/questions/17088091/get-sum-of-objects-before-with-linq

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入