Thursday, March 31, 2011

How to calculate sum of a DataTable's Column in LINQ (to Dataset)?

I'm just started to read up on LINQ and I want to start incorporating it into my code. I know how to compute the sum of a DataTable's column by either "Foreach"-ing through the rows or by doing a compute.sum on the specific column. How do I do the equivalent with LINQ to DataSet?

From stackoverflow
  • 101 Linq Samples

  • If untyped (replace int with the correct data type):

     var sum = table.AsEnumerable().Sum(x=>x.Field<int>(3));
    

    or:

     var sum = table.AsEnumerable().Sum(x=>x.Field<int>("SomeProperty"));
    

    If typed:

     var sum = table.Sum(x=>x.SomeProperty);
    
    rivera.reyrivera : That did the trick. Thank you very much
  • How to achieve this with Linq to XML Thanks. There is nothiug like TABLE

  • what if I have to add a where clause to the query?

0 comments:

Post a Comment