开发者

Linq to SQL Sum()

select sum(Sales) from WeeklyProductSale. how 开发者_开发百科i can write this query in linq and what this query will return to me.

thankas in advance.


VB.Net

Dim sumOfSales = Aggregate wps In db.WeeklyProductSale Into Sum(wps.Sales)


You can write it like this,

YourDBContextObject.WeeklyProductSales.Sum(w => w.Sales);


var answer = db.WeeklyProductSales.Sum (s => s.Sales);

This will return a variable of type s.Sales (e.g., decimal).

Edit: if you querying a DataTable, you will need to do this:

myDataTable.AsEnumerable().Sum (row => (decimal) row["Sales"]).

Make sure you reference System.Data.DataSetExtensions.dll and put "using System.Data.DataSetExtensions" into your program because the AsEnumerable extension method that this query uses relies on this. Change (decimal) to the type of the Sales column.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜