2014-01-23 92 views
0

我在使用c#编码的逻辑中遇到了一些问题。我有一个列表,它将所有物品存储在标准包装单元中。当我通过列表循环时,如果检测到某个项目库存不足,我会得到该项目的类别。之后,我传递给一个方法,按降序排序某个类别的项目。然后,我通过降序顺序列表循环以获得库存最高的顶级产品,并且我将以网格视图的形式显示该项目作为建议项目。这里是我建立了我的建议的网格视图:将方法从一种方法传递到另一种时的逻辑

<asp:GridView ID="gvSuggested" runat="server" AutoGenerateColumns="False" CellPadding="2" ForeColor="#333333" GridLines="None" Width="300px"> 
          <Columns> 
           <asp:BoundField DataField="name" HeaderText="Product Name" ItemStyle-Width="100px" /> 
           <asp:BoundField DataField="categoryName" HeaderText="Category" ItemStyle-Width="100px" /> 
          </Columns> 
         </asp:GridView> 

而后面的代码:

//Portion to check the storage level for each products stored in tempList 
     //Loop thru tempList. key as prod variant ID, tempList.Keys as quantity 
     foreach (string key in tempList.Keys) 
     { 
      //Get total unit of each products 
      totalUnit = prodPackBLL.getTotalProductUnit(key); 
      valid = true; 

      //Check if unitQuantity exceed storage level 
      if (((Convert.ToInt32(tempList[key])) * packagesNeeded) > totalUnit) 
      { 
       //Get the label control in gridview 
       foreach (GridViewRow gr in gvFinalised.Rows) 
       { 
        if (key == gvFinalised.DataKeys[gr.RowIndex].Value.ToString()) 
        { 
         //Change the color of textBox and display the insufficient message 
         valid = false; 
         //Automatically uncheck the checkBox if invalid 
         TextBox tb = (TextBox)gr.FindControl("tbQuantity") as TextBox; 
         tb.CssClass = "alert alert-danger"; 
         tb.Attributes["style"] = "height: 3px; width: 50px; margin-bottom: 0px; padding-left: 0px"; 
         Label lblCheckAmount = gr.FindControl("lblCheckAmount") as Label; 
         lblCheckAmount.Text = "Insufficient stock!"; 

         getSuggested(key); 
        } 
       } 
      } 

如果库存不足,调用getSuggested()通过传递ID一起:

protected void getSuggested(string prodVariantID) 
    { 
     string categoryName = prodPackBLL.getCategoryByProdVariantID(prodVariantID); 

     //Get the list of substitute product with highest storage level sorted in descending order 
     List<ProductPacking> prodSubstitute = new List<ProductPacking>(); 
     List<string> lstCategory = new List<string>(); 
     List<string> prodIDList = new List<string>(); 
     List<DistributionStandardPackingUnitItems> distSPUItem = new List<DistributionStandardPackingUnitItems>(); 

     //Find list of substitute with highest stock level and replace the product 
     prodSubstitute = prodPackBLL.getProductIDWithHighestStock(categoryName); 

     for (int count = 0; count < prodSubstitute.Count; count++) 
     { 
      //To prevent duplication of same product and select those catories which are in current category and counting them and taking them if there are less than 1 occurrences 
      if (!prodIDList.Contains(prodSubstitute[count].id) && !(lstCategory.Where(x => x.Equals(categoryName)).Select(x => x).Count() >= 1)) 
      { 
       prodIDList.Add(prodSubstitute[count].id); 
       lstCategory.Add(categoryName); 
      } 
     } 

     for (int j = 0; j < prodIDList.Count; j++) 
     { 
      //Get the detail of the product added into prodList and add it into distSPUItem List 
      distSPUItem.Add(packBLL.getSPUItemDetailByID(prodIDList[j])); 
     } 

     gvSuggested.DataSource = distSPUItemList; 
     gvSuggested.DataBind(); 
    } 

SQL方法获得最高产品库存水平的降序:

public List<ProductPacking> getProductIDWithHighestStock(string categoryName) 
    { 
     List<ProductPacking> prodSubstitute = new List<ProductPacking>(); 

     using (var connection = new SqlConnection(FoodBankDB.connectionString)) 
     { 
      SqlCommand command = new SqlCommand("SELECT p.id, p.inventoryQuantity FROM dbo.Products p " + 
       " INNER JOIN dbo.ProductVariants pv ON p.id = pv.product " + 
       " INNER JOIN dbo.ProductCategories pc ON p.productCategory = pc.id " + 
       " WHERE pc.categoryName = '" + categoryName + "' " + 
       " ORDER BY Convert(INT, p.inventoryQuantity) DESC", connection); 
      connection.Open(); 
      using (var dr = command.ExecuteReader()) 
      { 
       while (dr.Read()) 
       { 
        string prodID = dr["id"].ToString(); 

        prodSubstitute.Add(new ProductPacking(prodID)); 
       } 
      } 
     } 

     return prodSubstitute; 
    } 
显示在GridView中

SQL方法来获取项目的详细建议:

public DistributionStandardPackingUnitItems getSPUItemDetailByID(string prodID) 
    { 
     DistributionStandardPackingUnitItems item = new DistributionStandardPackingUnitItems(); 

     using (var connection = new SqlConnection(FoodBankDB.connectionString)) 
     { 
      SqlCommand command = new SqlCommand("SELECT p.id, p.name, p.description, pc.categoryName FROM dbo.Products p " + 
       " INNER JOIN dbo.ProductCategories pc ON p.productCategory = pc.id " + 
       " WHERE p.id = '" + prodID + "'" + 
       " ORDER BY pc.categoryName ", connection); 
      connection.Open(); 
      using (var dr = command.ExecuteReader()) 
      { 
       while (dr.Read()) 
       { 
        string id = dr["id"].ToString(); 
        string name = dr["name"].ToString(); 
        string description = dr["description"].ToString(); 
        string categoryName = dr["categoryName"].ToString(); 

        item = new DistributionStandardPackingUnitItems(id, name, description, categoryName, ""); 
       } 
      } 
     } 

     return item; 
    } 

然而,当我运行程序,它给了我一个错误信息:字段或属性名称为“名”未在所选数据源中找到。我不知道为什么当我以调试模式运行时,它确实返回了我所有的值。只是它不会在网格视图中显示。

在此先感谢。

编辑

比方说,我加入产品1,2,3-到列表中,而产品1是具有最高库存水平的产品。产品2和3不够。因此,当我执行它时,由于产品1已经在列表中,所以系统应该找到第二高的产品并替换产品2.在产品2被替换之后,系统应该替换第三高的产品3。我的prodSubstitute已经按照降序排列,我只是在for循环里面怀疑if语句,我该如何实现这个逻辑呢?

+0

采取的第n个最高量您是否尝试过只运行SQL,看看问题出在那里? – andreasnico

+0

SQL返回了我想要的东西。我想知道是因为我的价值传递吗? –

+0

类DistributionStandardPackingUnitItems是怎么样的? – andreasnico

回答

1

由于prodSubstitute已经被inventoryQuantity降序排序,您可以通过使用prodSubstitute[n-1]

+0

对不起,但我该如何实现?它是否将它放在if语句中,并且每次执行if时都会增加? –

+0

你能澄清一下吗? – ekad

+0

if(!prodIDList.Contains(prodSubstitute [count])。id)&&!(lstCategory.Where(x => x.Equals(categoryName))。Select(x => x).Count()> = 1)&& prodSubstitute [count] -1)?我是否以错误的方式行事?因为它没有意义,虽然 –

相关问题