c# - 如何避免循环并重构 C# 代码

标签 c#

我的用例是,我有一个需要发布到外部 API 的订单列表。 但条件是,我可以在 API 的一次调用后发布 5 个订单。 这 5 个订单必须来自同一家商店,并且所有 5 个订单的 deliveryWindows 应该是上午或下午。

我已经编写了以下代码,但我对此不满意,任何人都可以帮助重构以下逻辑。 我使用了 3 个 for 循环来遍历 Deliverywindowstores 以及商店中的所有 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; }
    }


最佳答案

正如所指出的,这个 post是一个很好的资源,可以帮助您了解如何针对多个键进行分组。

在您的情况下,情况如下:

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

关于c# - 如何避免循环并重构 C# 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72955554/

相关文章:

c# - 在 C# (winforms) 中如何同步文本框和 datagridview,以便一个中的更改显示在另一个中

c# - C# 十进制类型除法的精度在 Mono 上不同吗?

c# - 从 ASP.NET MVC Controller 中删除功能

c# - 上传用户头像图片

c# - 在没有帮助程序类的情况下从流中读取和写入字符串

C#通过键列表从字典中获取值子集

c# - ASP.NET 和 Facebook Connect - 如何使用 Graph API 发布到用户的墙上?

c# - 我不明白 "protected void Page_Load"是什么

c# - 用于验证逻辑 && || 的正则表达式字符串中的运算符

c# - .NET JIT 如何优化生成的代码布局?