LINQ Aggregate function to current line

Assuming I have

var list = new []{
            new { Price = 1000, IsFirst = true}, 
            new { Price = 1100, IsFirst = false},
            new { Price = 450, IsFirst = true},
            new { Price = 300, IsFirst = false}
};

and I want to generate the following output:

Price  IsFirst  First Second Final
----------------------------------
1000   True     1000  0      1000
1100   False    0     1100   -100
450    True     450   0      350
300    False    0     300    50

Is it possible for some kind of summary function to be processed before the current line? I like to have all things in pure LINQ, but at the moment I have no choice but to manually iterate over the list and summarize the column conditionally.

var result = list.Select(x => new 
{
    Price = x.Price,
    IsFirst = x.IsFirst,
    First = x.IsFirst ? x.Price : 0,
    Second = !x.IsFirst ? x.Price : 0,
    Final = 0 // ???
}).ToList();

int sum = 0;

for(int i=0; i<result.Count(); i++)
{
    sum += (result[i].IsFirst ? result[i].Price : - result[i].Price);   
    // updating Final value manually
}
+4
source share
3 answers

The easiest way to do this is to use the Microsoft Reactive Extension interactive extension method Scan. (Use NuGet and find Ix-Main.)

var query =
    list
        .Scan(new
        {
            Price = 0,
            IsFirst = true,
            First = 0,
            Second = 0,
            Final = 0
        }, (a, x) => new
        {
            Price = x.Price,
            IsFirst = x.IsFirst,
            First = x.IsFirst ? x.Price : 0,
            Second = !x.IsFirst ? x.Price : 0,
            Final = a.Final + (x.IsFirst ? x.Price : - x.Price)
        });

This gives:

result

However, you can do this with the built-in statement Aggregateas follows:

var query =
    list
        .Aggregate(new []
        {
            new
                {
                    Price = 0,
                    IsFirst = true,
                    First = 0,
                    Second = 0,
                    Final = 0
                }
        }.ToList(), (a, x) =>
        {
            a.Add(new
            {
                Price = x.Price,
                IsFirst = x.IsFirst,
                First = x.IsFirst ? x.Price : 0,
                Second = !x.IsFirst ? x.Price : 0,
                Final = a.Last().Final + (x.IsFirst ? x.Price : - x.Price)
            });
            return a;
        })
        .Skip(1);

You will get the same result.

+3

, , .

, LINQ , . , , :

, ,

  • IsFirst = true,
  • ,
  • .
+1

You can do something like this:

int final = 0;
var result = list.Select(x => new
{
    Price = x.Price,
    IsFirst = x.IsFirst,
    First = x.IsFirst ? x.Price : 0,
    Second = !x.IsFirst ? x.Price : 0,
    Final = x.IsFirst ? final+=x.Price : final-=x.Price
}).ToList();

But you need to define an integer (final) outside the linq expression to keep track of the total.

0
source

Source: https://habr.com/ru/post/1548240/


All Articles