简体   繁体   中英

Repeated subtraction of rows from a column using LINQ and C#

I would like to repeatedly subtract rows from a column in a table in my database using LINQ. My table is as shown

ID       Numbers

1         488  
2         612 
3         803 
4        1082 
5        1310 
6        1586 
7        1899 

I'd like to take 612 and subtract it with 488 and store the value.

Afterwards take 803 and subtract it with 612 and store the value.

Do that for every number

1082 - 803

1310 - 1082

1586 - 1310

1899 - 1586 

At the end i'd like to sum the obtained values.

Is there a specific function i could use? I've tried using for loop but i just can't seem to manage to put the pieces together for it to work. Can someone help?

Try this:

var numbers = new []
{
    488, 612, 803, 1082, 1310, 1586, 1899,
};

var deltas = numbers.Skip(1).Zip(numbers, (n1, n0) => n1 - n0);

var sum = deltas.Sum();

That gives:

124 
191 
279 
228 
276 
313 

With the sum of 1,411 .

var array = new []{ 488, 612, 803, 1082, 1310, 1586, 1899 };
var results=array.Last()-array.First();

Updated:

Your question can be simplified down to the above. It will give the correct answer because (x2-x1)+(x3-x2)+(x4-x3)+(x5-x4)+(x6-x5)+(x7-x6) can be simplifed to just -x1+x7 or x7-x1.

Given your original sample set, the answer is 1,411 .

If you want to do it the long way, you can do this:

var array = new []{ 488, 612, 803, 1082, 1310, 1586, 1899 };
var results=array.Skip(1).Select((e,i)=>e-array[i]).Sum();

You could also do it this way:

var array = new []{ 488, 612, 803, 1082, 1310, 1586, 1899 };
var results=array
  .Aggregate(new {prev=array.First(),sum=0},                            // Initialize
    (last,current)=>new {prev=current,sum=last.sum+current-last.prev},  // Repeat
    c=>c.sum);                                                          // Finalize

This is very simple. Just get total sum of item - previous item

private int GetSumOfSubtractions(int[] numbers)
{
    if (numbers.Length == 0) return 0;
    if (numbers.Length == 1) return numbers[0];
    int sum = 0;
    for (int i = 1; i < numbers.Length; i++)
    {
        sum += numbers[i] - numbers[i - 1];
    }
    return sum;
}

A little math behind this.

612 - 488 + 803 - 612 + 1082 - 803 + 1310 - 1082 + 1586 - 1310 + 1899 - 1586
^           ^^    ^            ^^

As you can see middle numbers cancel each other and become 0 and only first item and last item remains.

So 1899-488 is the answer.

Not sure if it's a good idea but you can try LinkedList<T> class in C#:

var numbers = new LinkedList<int>(new[]
                        {
                            488, 612, 803, 1082, 1310, 1586, 1899
                        });
            var sum = 0;
            var node = numbers.First;
            while (node.Next != null)
            {
                var cur = node;
                node = node.Next;
                Console.WriteLine(node.Value - cur.Value);
                sum += node.Value - cur.Value;
            }
            Console.WriteLine("********************");
            Console.WriteLine($"Sum is : {sum}");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM