Reputation: 1
i have table contain numeric fields and I want sum of the numeric field column using LINQ
Upvotes: 0
Views: 706
Reputation: 10005
from 101 Examples:
int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };
double numSum = numbers.Sum();
or..
string[] words = { "cherry", "apple", "blueberry" };
double totalChars = words.Sum(w => w.Length);
Upvotes: 3
Reputation: 15545
Dim numbers() As Integer = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0}
Dim numSum = numbers.Sum()
This is vb, try to convert in c# or as your specific language.
Upvotes: 0
Reputation: 474
Assuming "table" is a DataTable and "field" the numeric field, it is possibile to do like this:
sum = table.AsEnumerable().Sum(row => row.IsNull("field") ? 0.0 : (Double)row["field"]);
Here I used Double as numeric type.
Upvotes: 0
Reputation: 60694
Not many details in your question, but something like this should work (if the table is supposed to be a database table, and you have generated the data context):
var sum = myDataContext.MyTable.Sum( v => v.MyColumnToSum );
Upvotes: 0