簡體   English   中英

如何避免循環和重構 C# 中的代碼

[英]How to avoid Looping and refactor the Code in C#

我的用例是,我有一個需要發布到外部 API 的訂單列表。 但條件是,我可以在一次 API 調用中發布 5 個訂單。 並且這 5 個訂單必須在同一家商店,並且所有 5 個訂單的deliveryWindows時間窗口應該是上午或下午。

我已經編寫了以下代碼,但對此我不滿意,任何人都可以幫助重構以下邏輯。 我已經使用了 3 個 for 循環來遍歷Deliverywindow以及stores和商店中的所有orders

是否有更好的方法/下面的異步循環/具有單獨的方法調用。

任何建議都非常有幫助!

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


正如所指出的,這篇文章是一個很好的資源,可以幫助您了解如何針對多個鍵進行分組。

以下是您的情況:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM