Solved

Sum of a Column

Posted on 2012-04-07
5
355 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

920 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

15 Experts available now in Live!

Get 1:1 Help Now