[英]Linq query to count and group items by date
我有從EF提取的Orders
集合。 每個Order
都有一個訂單日期:
public class Order {
public DateTime Date { get; set; }
public int Id { get; set; }
}
我希望能夠運行查詢以返回特定日期范圍內每天的訂單數量。 查詢方法應類似於:
public class ICollection<OrderDateSummary> GetOrderTotalsForDateRange(DateTime startDate, DateTime endDate) {
var orderDateSummary = Set.SelectMany(u => u.Orders) // ..... grouping/totalling here?!
return orderDateSummary;
}
對於信息, Set
實際上是返回用戶聚合根的存儲庫的一部分,因此Set
的類型為DbSet<User>
我DbSet<User>
問題是對SelectMany
方法可查詢的Orders
分組和總計。
OrderDateSummary類如下所示:
public OrderDateSummary {
DateTime Date { get; set; }
int Total { get; set; }
}
因此,開始日期為01/01/2016和結束日期為03/01/2016的輸出將類似於:
Date Total
===================
01/01/2016 10
02/01/2016 2
03/01/2016 0
04/01/2016 12
var startDate = new DateTime (2016, 1, 1);
var endDate = new DateTime (2016, 1, 4);
Set.SelectMany(u => u.Orders).
Where (order => startDate <= order.Date && order.Date <= endDate) // If filter needed
GroupBy (order => order.Date, (date, values) =>
new OrderDateSummary () {
Date = date,
Total = values.Count ()
}).
OrderBy (summary => summary.Date).
ToList ();
只是您應該用class
或struct
標記OrderDateSummary
並將這些屬性設為public
或添加構造函數。
而且您的預期日期是04/01/2016,所以,我想您的結束時間是4日而不是3日。
怎么樣
List<OrderDateSummary> Result = OrderList
.Where(x => x.Date >= startDate && x.Date <= endDate)
.GroupBy(x => x.Date)
.Select(z => new OrderDateSummary(){
Date = z.Key,
Total = z.Count()
}).OrderBy(d=> d.Date).ToList();
如我所見,您需要生成從start
到end
所有日期。 然后計算每個日期的訂單總數。
DateTime start = new DateTime(2016, 1, 1);
DateTime end = new DateTime(2016, 1, 4);
Enumerable
.Range(0, 1 + (end - start).Days)
.Select(x => start.AddDays(x))
.GroupJoin(Set.SelectMany(u => u.Orders),
dt => dt, o => o.Date.Date,
(dt, orders) => new OrderDateSummary { Date = dt, Total = orders.Count() })
.ToList();
查看有關Ideone的工作示例 。
嘗試下面的代碼是linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace ConsoleApplication82
{
class Program
{
static void Main(string[] args)
{
List<OrderDateSummary> orderSummary = null;
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("date", typeof(DateTime));
dt.Columns.Add("amount", typeof(decimal));
dt.Rows.Add(new object[] { 1, DateTime.Parse("1/1/16"), 1.00 });
dt.Rows.Add(new object[] { 2, DateTime.Parse("1/1/16"), 2.00 });
dt.Rows.Add(new object[] { 3, DateTime.Parse("1/2/16"), 3.00 });
dt.Rows.Add(new object[] { 4, DateTime.Parse("1/2/16"), 4.00 });
dt.Rows.Add(new object[] { 5, DateTime.Parse("1/2/16"), 5.00 });
dt.Rows.Add(new object[] { 6, DateTime.Parse("1/3/16"), 6.00 });
dt.Rows.Add(new object[] { 7, DateTime.Parse("1/3/16"), 7.00 });
orderSummary = dt.AsEnumerable()
.GroupBy(x => x.Field<DateTime>("date"))
.Select(x => new OrderDateSummary() { Date = x.Key, Total = x.Count() })
.ToList();
}
}
public class OrderDateSummary {
public DateTime Date { get; set; }
public int Total { get; set; }
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.