简体   繁体   中英

How to avoid Looping and refactor the Code in C#

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; }
    }


As pointed out, this post is a great resource to help you understand how to group against multiple keys.

Here's what that would look like in your case:

var allOrders = GetProductOrders();

var groupedOrders = from order in allOrders
                    // We group using an anonymous object
                    // that contains the properties we're interested in
                    group order by new 
                    { 
                       order.StoreName, 
                       order.DeliveryWindow 
                    };

// Access is straightforward:
foreach (var orderGroup in groupedOrders)
{
  Console.WriteLine($"Group {orderGroup.Key.StoreName} {orderGroup.Key.DeliveryWindow}");
  // The group is a list itself, so you can apply
  // your Batch LINQ extension
  foreach (var order in orderGroup)
  {
    Console.WriteLine(order.ID);
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM