none
LINQ to SQL - Loop through columns of a DB RRS feed

  • Question

  • Hi,

    Thanks to Cole Wu, I can now sum partial column (Order by date) of a Db with the following code:

                using (var db = new SampleDataContext())
                {
                    int nX = x;
                    var table = (from p in db.DonneesBinomiales
                                orderby p.Date descending
                                select p).Take(nX);

                    var sum1 = table.Sum(i => i._1);
                }

    where "_1" is the name of my first column...but...my database contains a lot of columns and I would like to loop across each columns and avoid a code like:

    ...

    var sum1 = table.Sum(i => i._1);

    var sum2 = table.Sum(i => i._2);

    ...

    var sumn = table.Sum(i => i._n);

    In addition to this, the name of my columns are not always in the form "_n"!

    Any idea?


    • Edited by Patonline Monday, June 27, 2016 9:40 PM
    Monday, June 27, 2016 9:39 PM

Answers

  • Hi Patonline,

    >>where "_1" is the name of my first column...but...my database contains a lot of columns and I would like to loop across each columns

    Please refer to the following code about multiple sum columns.

    using (var db =new EFDemoContext())
                {
                    var result = db.DonneesBinomiales
                       .GroupBy(d => 0)
                       .Select(r => new
                       {
                           sum1 = r.Sum(x => x.Number),
                           sum2 = r.Sum(x => x.Number2),
                           sum3  =r.Sum(x=>x.Number3)
                       })
                       .FirstOrDefault();
    
    }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Tuesday, June 28, 2016 2:27 AM
    Moderator

All replies

  • Hi Patonline,

    >>where "_1" is the name of my first column...but...my database contains a lot of columns and I would like to loop across each columns

    Please refer to the following code about multiple sum columns.

    using (var db =new EFDemoContext())
                {
                    var result = db.DonneesBinomiales
                       .GroupBy(d => 0)
                       .Select(r => new
                       {
                           sum1 = r.Sum(x => x.Number),
                           sum2 = r.Sum(x => x.Number2),
                           sum3  =r.Sum(x=>x.Number3)
                       })
                       .FirstOrDefault();
    
    }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Tuesday, June 28, 2016 2:27 AM
    Moderator
  • Hello Cole Wu,

    It is very kind of you!

    Is your code designed to make a sum of a group of column?

    Because, what I tried to do is an individual sum of a first column (sum1), store it in an array or in an other db, go to the second column, calculate a second sum (sum2) and store it in an array or in an other db, etc. until n column.

    Sorry to disturb you! Thanks a lot for your help,

    Best regards, Patrick


    • Edited by Patonline Tuesday, June 28, 2016 11:05 AM
    Tuesday, June 28, 2016 11:05 AM
  • Hi Patonline,

    >>Is your code designed to make a sum of a group of column?

    It doesn't mark a sum of a group of any special column. please try to test the code and check if you retrieve the result as you want.

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, July 7, 2016 1:11 AM
    Moderator