0

How would I do a linq query like this For example I want to count up all the numbers in a column and for it to return the result of them being added up

Thanks

James
  • 11
  • 1
  • 2
  • 1
    possible duplicate of [Sum of items in a collection](http://stackoverflow.com/questions/61870/sum-of-items-in-a-collection) – womp Mar 24 '11 at 21:16

4 Answers4

1

var count = mytable.Sum(t => t.myCol);

Brent Stewart
  • 1,830
  • 14
  • 21
1

I find the LINQ to SQL samples site on MSDN to be an excellent reference for basic questions like this.

The Simple Sum example there is probably what you want:

// This sample uses Sum to find the total freight over all Orders.
void LinqToSqlCount03()
{
    var q =  (from o In db.Orders 
             select o.Freight).Sum()
}
LBushkin
  • 129,300
  • 32
  • 216
  • 265
0

Here we go:

 DbContextName = dbTableName.Count(x=>x.columnName);
Husni Salax
  • 1,968
  • 1
  • 19
  • 29
0

Try the following:

numbers.Sum(n => n.Value);
Servy
  • 202,030
  • 26
  • 332
  • 449
Josh M.
  • 26,437
  • 24
  • 119
  • 200