Solved

Sum of a Column

Posted on 2012-04-07
5
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

636 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