如何为通用逻辑编写单独的私有方法

我已经用多个if条件重复了代码,但我不想在每个if块中写该代码..我需要使用一个私有方法来调用该代码,但无法声明方法的反对,请让我现在如何使用波纹管代码进行定义 实体班

   public class Order
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int OrderId { get; set; }
        public Guid OrderGuid { get; set; }
        .
        .
        .
         public virtual IList<OrderItem> OrderItems { get; set; }
      }


  public class OrderItem
        {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int OrderItemId { get; set; }
        public Guid OrderItemGuid { get; set; }
        public int OrderId { get; set; }
       .
       .
       .
       public virtual Order Order { get; set; }
}

我的逻辑是

List<Contracts.Models.ProductReportResponse> resultList = new List<Contracts.Models.ProductReportResponse>();    
List<Contracts.Entities.Order> orders = await this.DbSet.Include(t => t.OrderItems).ToList();
 ordersInfo = orders.GroupBy(x => x.StoreId.ToString()).AsQueryable();
                    foreach (IGrouping<string, Order> order in ordersInfo)
                    {
                        foreach (Order info in order)
                        {
                            for (int i = 0; i < info.OrderItems.Count; i++)
                            {
                                if (!string.IsNullOrEmpty(request.ProductName) && info.OrderItems[i].ProductName == request.ProductName && string.IsNullOrEmpty(request.ProductCode) && request.Purity == 0m )
                                {
                                    ProductReportResponse productReport = resultList.FirstOrDefault(a => a.City == info.BillingCity && a.StoreId == info.StoreId && a.StateId == info.BillingState &&
                                    a.Purity == info.OrderItems[i].PTouch && a.SKU == info.OrderItems[i].SKU && a.ProductName == info.OrderItems[i].ProductName) ?? new ProductReportResponse();
                                    if (productReport.ProductName != null && resultList.Count != 0)
                                    {
                                        productReport.Quantity += info.OrderItems[i].Quantity;
                                        productReport.Weight += info.OrderItems[i].ItemWeight;
                                        productReport.TotalOrders += 1;
                                        productReport.TotalAmount += Math.Round((info.OrderItems[i].TMC) + (info.OrderItems[i].ProductCost) * (info.OrderItems[i].Purity), 2);
                                    }
                                    else
                                    {
                                        productReport.ProductName = info.OrderItems[i].ProductName;
                                        productReport.StoreName = storeRepository.GetStoreName(info.StoreId).Result;
                                        productReport.StoreId = info.StoreId;
                                        productReport.SKU = info.OrderItems[i].SKU;
                                        productReport.City = info.BillingCity;
                                        productReport.State = stateProvinceRepository.GetStateName(Int16.Parse(info.BillingState)).Result;
                                        productReport.StateId = info.BillingState;
                                        productReport.Purity = info.OrderItems[i].PTouch;
                                        productReport.Quantity = info.OrderItems[i].Quantity;
                                        productReport.Weight = info.OrderItems[i].ItemWeight;
                                        productReport.TotalOrders = 1;
                                        productReport.TotalAmount = Math.Round((info.OrderItems[i].TMC) + (info.OrderItems[i].ProductCost) * (info.OrderItems[i].Purity), 2);   
                                      resultList.Add(productReport);
                                    }
                                }    
                            }

                         }
                           resultList = resultList.ToList();
             }

在if块逻辑中,我需要应用另一个if块条件..所以我不想再次编写该代码。所以需要从私有方法调用,但我不明白如何将参数对象传递给我写的方法

 private Task<List<ProductReportResponse>> ProductReportsFilter( ????)
        {

            ProductReportResponse productReport = resultList.FirstOrDefault(a => a.City == info.BillingCity && a.StoreId == info.StoreId && a.StateId == info.BillingState &&
                                    a.Purity == info.OrderItems[i].PTouch && a.SKU == info.OrderItems[i].SKU && a.ProductName == info.OrderItems[i].ProductName) ?? new ProductReportResponse();
                                    if (productReport.ProductName != null && resultList.Count != 0)
                                    {
                                        productReport.Quantity += info.OrderItems[i].Quantity;
                                        productReport.Weight += info.OrderItems[i].ItemWeight;
                                        productReport.TotalOrders += 1;
                                        productReport.TotalAmount += Math.Round((info.OrderItems[i].TMC) + (info.OrderItems[i].ProductCost) * (info.OrderItems[i].Purity), 2);
                                    }
                                    else
                                    {
                                        productReport.ProductName = info.OrderItems[i].ProductName;
                                        productReport.StoreName = storeRepository.GetStoreName(info.StoreId).Result;
                                        productReport.StoreId = info.StoreId;
                                        productReport.SKU = info.OrderItems[i].SKU;
                                        productReport.City = info.BillingCity;
                                        productReport.State = stateProvinceRepository.GetStateName(Int16.Parse(info.BillingState)).Result;
                                        productReport.StateId = info.BillingState;
                                        productReport.Purity = info.OrderItems[i].PTouch;
                                        productReport.Quantity = info.OrderItems[i].Quantity;
                                        productReport.Weight = info.OrderItems[i].ItemWeight;
                                        productReport.TotalOrders = 1;
                                        productReport.TotalAmount = Math.Round((info.OrderItems[i].TMC) + (info.OrderItems[i].ProductCost) * (info.OrderItems[i].Purity), 2);

                                        resultList.Add(productReport);
                                    }

            resultList = resultList.ToList();
            return Task.FromResult(resultList);
        }

如果块是波纹管的预期结果

 if (!string.IsNullOrEmpty(request.ProductName) && info.OrderItems[i].ProductName == request.ProductName && string.IsNullOrEmpty(request.ProductCode) && request.Purity == 0m )
   {
         resultList  = this. ProductReportsFilter( ????);
   }
评论