Solved

Sum of a Column

Posted on 2012-04-07
5
353 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
Comment Utility
use the code below:-
objCtx.MyBalances.Sum(b=>b.Balance);

Open in new window

0
 

Author Comment

by:Whing Dela Cruz
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now