My use case is, I have a List of orders that I need to post to external API.
but the conditions are that, I can post 5 order in one post call of API.
and these 5 orders have to be of same store and of deliveryWindows
should be either morning or afternoon for all 5 orders.
I have written the below code but I am not happy with that, Can anyone Kindly help to refactor the below logic.
I have used 3 for loops to Loop through Deliverywindow
and also for stores
and for all the orders
in the store.
Is there better approach/async Looping of the below/ having separate method calls.
Any suggestion is really helpful!
using MoreLinq;
using System;
using System.Collections.Generic;
using System.Linq;
static void Main(string[] args)
{
//In real time I will have 1000's of orders for one store and deliveryWindow (Morning)
var allOrders = GetProductOrders();
string[] deliveryWindows = new[] { "Morning", "AfterNoon" };
//Looping for Morning & Afternoon
foreach (var deliveryWindow in deliveryWindows)
{
//Getting Morning order in first run and afternoon order in second run
var OrderForWindow = allOrders.Where(x => x.DeliveryWindow.Equals(deliveryWindow));
//Getting All Unique Store (Will have StoreA, StoreB, etc)
List<string> Stores = OrderForWindow.Select(x => x.StoreName).Distinct().ToList();
foreach (var Store in Stores)
{
//Store releated order for that windown (morning/afternoon)
var StoreOrders = OrderForWindow.Where(order => order.StoreName.Equals(Store)).ToList();
//taking 10 items from StoreOrders
//Batch will pick 5 items at once
foreach (var orders in StoreOrders.Batch(5))
{
//storeOrder will have list of 5 order which all have same delivery window
//Post External call
}
}
}
}
public static List<ProductOrder> GetProductOrders()
{
List<ProductOrder> productOrder = new List<ProductOrder>()
{
new ProductOrder(){ ID = 1, DeliveryWindow ="Morning", StoreName = "StoreA", customerDetails = "Cust1"},
new ProductOrder(){ ID = 2, DeliveryWindow ="Morning", StoreName = "StoreA",customerDetails = "Cust2"},
new ProductOrder(){ ID = 3, DeliveryWindow ="Morning", StoreName = "StoreA",customerDetails = "Cust3"},
new ProductOrder(){ ID = 4, DeliveryWindow ="AfterNoon", StoreName = "StoreA",customerDetails = "Cust4"},
new ProductOrder(){ ID = 5, DeliveryWindow ="AfterNoon", StoreName = "StoreA",customerDetails = "Cust5"},
new ProductOrder(){ ID = 6, DeliveryWindow ="Morning", StoreName = "StoreB",customerDetails = "Cust6"},
new ProductOrder(){ ID = 7, DeliveryWindow ="Morning", StoreName = "StoreB",customerDetails = "Cust7"},
new ProductOrder(){ ID = 8, DeliveryWindow ="AfterNoon", StoreName = "StoreB",customerDetails = "Cust8"},
new ProductOrder(){ ID = 9, DeliveryWindow ="AfterNoon", StoreName = "StoreB",customerDetails = "Cust9"},
new ProductOrder(){ ID = 10, DeliveryWindow ="AfterNoon", StoreName = "StoreC",customerDetails = "Cust10"},
};
return productOrder;
}
}
public class ProductOrder
{
public int ID { set; get; }
public string StoreName { set;get;}
public string DeliveryWindow { set; get; }
public string customerDetails { set; get; }
public string ProductDetails { set; get; }
}