简体   繁体   中英

Linq Count all items in a column

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

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

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()
}

开始了:

 DbContextName = dbTableName.Count(x=>x.columnName);

请尝试以下方法:

numbers.Sum(n => n.Value);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM