Onl*_*ere 0 c# datetime iqueryable date-range
这是我当前的月份:
model.SalesForMonth = orders.Where(o => o.DateOfPayment.Value.Month == DateTime.Now.Month)
.Select(o => o.Total)
.Sum();
Run Code Online (Sandbox Code Playgroud)
这不符合预期,因为它也取得了去年同月发生的订单.
当然我可以比较年和月,但必须有一个我不熟悉的更具表现力的解决方案.
我怎样才能使用Linq干净利落地做到这一点?(不确定是否相关但是orders来自实体框架的IQueryable)
最简单的方法是创建一个起点和终点:
// TODO: Consider what time zone you want to consider the current date in
var today = DateTime.Today;
var start = new DateTime(today.Year, today.Month, 1);
var end = start.AddMonths(1); // Exclusive end-point
var query = orders.Where(o => o.DateOfPayment.Value >= start &&
o.DateOfPayment.Value < end)
.Sum(o => o.Total)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
146 次 |
| 最近记录: |