i have table contain numeric开发者_开发知识库 fields and I want sum of the numeric field column using LINQ
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);
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 );
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.
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.
精彩评论