locked
Subtract numbers from 7 rows in my database. RRS feed

  • Question

  • User-352524747 posted

     I would like to subtract numbers from 7 rows in my database. 

    My table looks like this:

    AB   | CD   | EF
    56.7 | 33.2 | 23. 5
    23.4 | 12.8 | 34.6
    12.3 | 33.7 | 19.10

    For each column AB = 12.3 - 23.4 and display the value on the side of the latest record (12.3), CD = 33.7 - 12.8 (display the value on 33.7) and so on for 7 rows.

    I use this query

    var latest = db.Query("SELECT TOP 7 * FROM Numbers ORDER BY Date DESC");

    and a foreach loop

    @foreach (var v in latest)
        {
            <li>
                <span>@v.Date.ToString("dd.MM.yyyy")</span>
                <span>@v.AB</span>
                <span>@v.CD</span>
                <span>@v.EF</span>
            </li>
        }

     

    Friday, November 4, 2016 3:39 PM

Answers

  • User-2057865890 posted

    Hi Dow7,

    You could use Aggregate.

    @{
        decimal[] intArray = new decimal[] { 2.1m, 3.2m, 4.1m, 6.3m };
    
        var result = new List<decimal>();
    
        intArray.Aggregate((x, y) => { result.Add(y - x); return y; });
    }
    @foreach (var item in result)
    {
        <li>
            <span>@item</span>
        </li>
    }

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 10, 2016 2:13 AM

All replies

  • User-2057865890 posted

    Hi Dow7,

    If you mean adding calculation results and display as latest record, you could try this.

    public class TestModel
        {
            public double AB { get; set; }
            public double CD { get; set; }
            public double EF { get; set; }
        }
    
    @{
        var latest = new List<TestModel>
            {
                new TestModel { AB=100, CD=100, EF=100 },
                new TestModel { AB=1, CD=2, EF=3 },
                new TestModel { AB=1, CD=1, EF=1 }
            };
    
        double valueA = 0;
        double valueB = 0;
        double valueC = 0;
    
        for (int i = 0; i < latest.Count; i++)
        {
            if(i==0)
            {
                valueA = latest[i].AB;
                valueB = latest[i].CD;
                valueC = latest[i].EF;
            }
            else
            {
                valueA -= latest[i].AB;
                valueB -= latest[i].CD;
                valueC -= latest[i].EF;
            }
        }
        latest.Add(new TestModel { AB = valueA, CD = valueB, EF = valueC });
    }
    
    
    @foreach (var v in latest)
    {
        <li>
            <span>@v.AB</span>
            <span>@v.CD</span>
            <span>@v.EF</span>
        </li>
    }

    Best Regards,

    Chris

    Monday, November 7, 2016 9:54 AM
  • User-352524747 posted

    I mean subtracting each value with the previous one from same column, on each column.

    AB
    2.1
    3.2
    4.1
    6.3

    I want to have this for AB

    AB
    2.1
    3.2 (3.2 - 2.1 = 1.1 -> display only result)
    4.1 (4.1 - 3.2 = 0.9)
    6.3 (6.3 - 4.1 = 2.2)

    ...for all other columns

    Monday, November 7, 2016 5:56 PM
  • User-2057865890 posted

    Hi Dow7,

    You could use Aggregate.

    @{
        decimal[] intArray = new decimal[] { 2.1m, 3.2m, 4.1m, 6.3m };
    
        var result = new List<decimal>();
    
        intArray.Aggregate((x, y) => { result.Add(y - x); return y; });
    }
    @foreach (var item in result)
    {
        <li>
            <span>@item</span>
        </li>
    }

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 10, 2016 2:13 AM