I need to calculate how many orders there are in total every day and print them in the datatable day by day. I have to perform this time allocation based on the OrderDate
value in my table. I need to find the total number of orders for each day.
I want to do:
Date | Total Order |
---|---|
09.10.2022 | 45 |
09.11.2022 | 60 |
09.12.2022 | 75 |
Order class:
public class Order : IBaseEntity
{
[Key]
public int OrderId { get; set; }
public int UserId { get; set; }
public int RestaurantId { get; set; }
public int AddressId { get; set; }
public int? PromotionId { get; set; }
public DateTime OrderDate { get; set; }
public DateTime? DeliveryDate { get; set; }
public OrderStatues OrderStatus { get; set; }
public bool DropDoor { get; set; }
public bool RingBell { get; set; }
public string OrderNote { get; set; }
public OrderTypes OrderType { get; set; }
public string PaymentType { get; set; }
public decimal SubTotal { get; set; }
public decimal DiscountTotal { get; set; }
public decimal OrderTotal { get; set; }
public decimal DeliveryCost { get; set; }
public OrderSources OrderSource { get; set; }
public OrderTimes OrderTime { get; set; }
public OrderDays OrderDay { get; set; }
[NotMapped]
public int[] SelectedProducts { get; set; }
[NotMapped]
public int OrderHour { get; set; }
public virtual Restaurant Restaurant { get; set; }
public virtual User User { get; set; }
public virtual Address Address { get; set; }
public virtual Promotion Promotion { get; set; }
public virtual ICollection<JoinOrderProduct> JoinOrderProducts { get; set; } = new HashSet<JoinOrderProduct>();
public void Build(ModelBuilder builder)
{
builder.Entity<Order>(entity =>
{
entity
.HasKey(p => new { p.OrderId});
entity
.HasOne(p => p.Restaurant)
.WithMany(p => p.Orders)
.HasForeignKey(p => p.RestaurantId)
.OnDelete(DeleteBehavior.Cascade);
entity
.Property(p => p.SubTotal)
.HasPrecision(18, 4)
.IsRequired();
entity
.Property(p => p.DiscountTotal)
.HasPrecision(18, 4)
.IsRequired();
entity
.Property(p => p.OrderTotal)
.HasPrecision(18, 4)
.IsRequired();
entity
.Property(p => p.DeliveryCost)
.HasPrecision(18, 4)
.IsRequired();
});
}
}
CodePudding user response:
I need to find the total number of orders for each day.
I assume that if two Order
objects ahave the same value for property Order.OrderDate.Date
, that you define them as two orders on the same day.
In this case, you have to group your Orders. Each group consists of Orders that are on one date, = that have the same value for property Order.OrderDate.Date
. Once you have these groups, you can count the number of Orders in each group, to know the number of Orders on that date.
For this I use the overload of Enumerable.GroupBy that has a parameter resultSelector
. I use the resultSelector to convert the groups into the values that you want.
IEnumerable<Order> orders = ...
// make groups of Orders that have the same order date
var result = orders.GroupBy(order => order.OrderDate.Date,
// parameter resultSelector: use the key (= the common date for all Orders in the group
// and all Orders in the group, to specify the requested output
(commonOrderDate, ordersWithThisDate) => new
{
Date = commonOrderDate,
TotalOrder = ordersWithThisDate.Count,
});
In words: from each order
in your sequence of Orders
, calculate the value of order.OrderDate.Date
(= key). Make groups of Orders
that have the same key value. For each key-group combination (= common value Orders that have this common value), make one resulting object. This resulting object has two properties:
- Date: the common value of all Orders in the group
- TotalOrder: the number of Orders in this group. This is the number of Orders that were place on this Date.