Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Sum of a Column

Posted on 2012-04-07
5
Medium Priority
?
373 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 1500 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] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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 information from SQL Server on Database, Connection and Server properties

721 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