我在使用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語句,我該如何實現這個邏輯呢?
採取的第n個最高量您是否嘗試過只運行SQL,看看問題出在那裏? – andreasnico
SQL返回了我想要的東西。我想知道是因爲我的價值傳遞嗎? –
類DistributionStandardPackingUnitItems是怎麼樣的? – andreasnico