Link to home
Start Free TrialLog in
Avatar of ram27
ram27

asked on

LINQ to sum the items in a list

Hi,

 I have a list which is of object type.
 my list contains the set of elements like
 
    Jan  3 ,4 5 , 6
    feb   3, 5 ,7,8

 now i need to sum the values and display in my grid as

   Jan  3 ,4 5 , 6
    feb   3, 5 ,7,8
   total  6  9  12 14
             
  how can i sum those values using LINQ and display everything in the grid

 Thanks
Avatar of ram27
ram27

ASKER

My property names in listobject as header s in the grid like

 
 RowHeader              pens           pencils      books        bags
  Jan                               3             ,4              5 ,           6
    feb                              3,              5 ,             7,           8

 
 Thanks
Avatar of kaufmed
If I understand your structure correctly, then I think you have something like this (for example):

List<Inventory> items = new List<Inventory>()
{
    new Inventory() { Month = "Jan", Pens = 3, Pencils = 4, Books = 5, Bags = 6 },
    new Inventory() { Month = "Feb", Pens = 3, Pencils = 5, Books = 7, Bags = 8 }
};

Open in new window


If so, you should be able to do something like this:

var totals = new
{
    Pens = items.Sum(item => item.Pens),
    Pencils = items.Sum(item => item.Pencils),
    Books = items.Sum(item => item.Books),
    Bags = items.Sum(item => item.Bags)
};

Open in new window


You should be able to data-bind the grid to that result. What kind of grid are you using?
Avatar of ram27

ASKER


 Thankyou,

 Is there is any way i can add the where condition,  becase i may get the the different values
 which i need to use in 3 differnt grids like below
 GRid 1:
 Current Statistics:

   RowHeader              pens           pencils      books        bags
  Jan                               3             ,4              5 ,           6
    feb                              3,              5 ,             7,           8

 
GRID 2:  
Moth Statistics

 RowHeader              pens           pencils      books        bags
  Jan                              6            7             8,                 9    
   feb                              3,              8 ,             7,           8

  I have these four above rows in my list and having a where condion for type = current and
 month

  I got a list now with four rows, is there any way i can write a LINQ query common to two grids
  to get the total row for both of them..


 

 
So do you have something like this (data-wise, not necessarily structure-wise)?
List<Inventory> items = new List<Inventory>()
            {
                new Inventory() { Month = "Jan", Type = "Current", Pens = 3, Pencils = 4, Books = 5, Bags = 6 },
                new Inventory() { Month = "Feb", Type = "Current", Pens = 3, Pencils = 5, Books = 7, Bags = 8 }
                new Inventory() { Month = "Jan", Type = "Old", Pens = 6, Pencils = 7, Books = 8, Bags = 9 },
                new Inventory() { Month = "Feb", Type = "Old", Pens = 3, Pencils = 8, Books = 7, Bags = 8 }
            };

Open in new window

Avatar of ram27

ASKER

Yes i had something like that in my list which consists all the rows for two grids

I want to calculate the total of them and display...

like below..

   RowHeader              pens           pencils      books        bags
  Jan                               3             ,4              5 ,           6
    feb                              3,              5 ,             7,           8
 tota                              6              9                12          14
 
GRID 2:  
Moth Statistics

 RowHeader              pens           pencils      books        bags
  Jan                              6            7             8,                 9    
   feb                              3,              8 ,             7,           8

  totoal                         9               15          15           17


How can i calculate the total of them seperately and display in the grid
so that the code will be common for both
For current, you could do something like this:

var current = items.Where(item => item.Type == "Current");

var totals = new
{
    Pens = current.Sum(item => item.Pens),
    Pencils = current.Sum(item => item.Pencils),
    Books = current.Sum(item => item.Books),
    Bags = current.Sum(item => item.Bags)
};

Open in new window


For monthly totals, if I'm interpreting your data correctly, you could use a "group by":

var results = from item in items
              group item by item.Month into g1
              select new
              {
                  Pens = items.Sum(item => item.Pens),
                  Pencils = items.Sum(item => item.Pencils),
                  Books = items.Sum(item => item.Books),
                  Bags = items.Sum(item => item.Bags)
              };

Open in new window

Avatar of ram27

ASKER

Hi, Thanks,

Is there any way i can totoal them in single line of code, because i have
12 grids like that..  
 I mean without specifying pens, pencils etc.. every time..
 I want to write a function common to do the total for all my grids..
 and in some Grids my properties will be different( mean gird headers). like

                    shoes         dress              socks
     wed          3             5                  6
     Thu          7              8                9
     totoal          10      13             15

 
 Thanks
ASKER CERTIFIED SOLUTION
Avatar of nixkuroi
nixkuroi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
BTW:  This example requires the System.Xml.Linq namespace.