开发者

C# Expressions.How to get Sum (Amount) for last month?

开发者 https://www.devze.com 2023-02-25 00:15 出处:网络
I hava a Transaction datatable with \"Amount\" field. I wan开发者_如何转开发t to get Sum (Amount) for 7 days

I hava a Transaction datatable with "Amount" field.

I wan开发者_如何转开发t to get Sum (Amount) for 7 days

How can I do it in C# expression?

Thank you


I assume your transaction table is called 'Transaction' and have a 'Date' field;

DateTime lastWeek = DateTime.Now.Subtract(new TimeSpan(7,0,0,0));
var amountSumLastWeek = (from t in Transaction
                        where t.Date >= lastWeek
                        select t.Amount).Sum();

EDIT: Of course const in C# means compile time constant and TimeSpan does not have optional parameters, so I have update the code

0

精彩评论

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