Go Premium for a chance to win a PS4. Enter to Win

x
Solved

# Sum of a Column

Posted on 2012-04-07
Medium Priority
376 Views
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());
}

0
Question by:Whing Dela Cruz
• 3
• 2

LVL 20

Accepted Solution

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

Author Comment

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

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

}

}
``````
0

Author Comment

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

ID: 37927789
Thanks!
0

## Featured Post

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
###### Suggested Courses
Course of the Month13 days, left to enroll

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

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