Solved

Sum of a Column

Posted on 2012-04-07
5
358 Views
Last Modified: 2012-05-03
How to get a Sum of a Column using C#, with Silverlight?

This is my Table
Name :         Balance
Rex               500.00
Alex              250.75
Roger            300.78

I want to get the Sum of the Balance. How?
I have a code here but I think there's something lacking.

        private void cmdUpdate_Click(object sender, RoutedEventArgs e)
        {
                objctx = new InOutOrganizationContext();
                var salary = objctx.MyBalances.Select(i => i.Balance);
                var sum = salary.Sum();
                MessageBox.Show(sum.ToString());
        }

Any help? please!
0
Comment
Question by:Whing Dela Cruz
  • 3
  • 2
5 Comments
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37818893
use the code below:-
objCtx.MyBalances.Sum(b=>b.Balance);

Open in new window

0
 

Author Comment

by:Whing Dela Cruz
ID: 37818929
Hi BuggyCoder,

I tried to used this code but the resul is (0) zero.
                var sum = objctx.MyBalances.Sum(b => b.Balance);
                MessageBox.Show(sum.ToString());
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37818966
Are you sure that your Balance Entity has values in the Balance Column or property, i have simulated your problem and my list of Balance Entity does return value when i apply sum on it:-

class MyBalance
    {
        public int Id { get; set; }
        public int Balance { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {

            var lstBalance = new List<MyBalance>
                                 {
                                     new MyBalance {Balance = 10, Id = 1},
                                     new MyBalance {Balance = 32, Id = 2},
                                     new MyBalance {Balance = 67, Id = 3}
                                 };

            var retVal = lstBalance.Sum(b => b.Balance); //Return 109 as sum
            

            Console.Read();

        }

        
    }

Open in new window

0
 

Author Comment

by:Whing Dela Cruz
ID: 37819052
I tried to check Balance Entity and it has Values, So, I tried to used the code given above and it is working... I am now trying to examine which part of my code has problem. Please advice me if you can see something... Thanks A lot BuggyCoder...
0
 

Author Closing Comment

by:Whing Dela Cruz
ID: 37927789
Thanks!
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question