locked
Add Column with Delta values RRS feed

  • Question

  • User-1971168174 posted

    Hi ,

    I need to add one more column in datatable using linq for calculating delta between values

    A Output Calculation
    25
    9 16 25-9=16
    78 69 9-78=69
    55 23 78-55=23
    33 22 55-33=22
    88 55 33-88=55

    Thanks for your help!

    Monday, January 8, 2018 6:26 AM

All replies

  • User1400794712 posted

    Hi neerajkumarmodi,

    Do you mean that, you want to using linq to set value for Output? I make a demo using EF, you can refer to it:

    public class Calculation
    {
        public int id { get; set; }
        public int A { get; set; }
        public int Output { get; set; }
    }

    </div> <div>

    context.Calculation.AddOrUpdate(
        new Models.Calculation { A = 25 },
        new Models.Calculation { A = 9 },
        new Models.Calculation { A = 78 },
        new Models.Calculation { A = 55 },
        new Models.Calculation { A = 33 },
        new Models.Calculation { A = 88 }
        );
    public ActionResult CalculationLinq()
    {
        var Alldata=db.Calculation.ToList();
        for(int i=1;i<Alldata.Count();i++)
        {
            Alldata[i].Output =Math.Abs(Alldata[i - 1].A - Alldata[i].A);
            db.SaveChanges();
        }
        return View();
    }

    Best Regards,

    Daisy

    Tuesday, January 9, 2018 11:46 AM