我正在做一个项目,其中我有三个表:库存,股票和购买。只有当库存表中的数量小于某一特定产品的库存表中的数量时,我才能购买单个产品。下面的代码也会阻碍其他产品的购买。比如:
Stock Table:
StoreID- ProductID - Quantity
1 - 1- 50
InventoryTable:
StoreID- ProductID - Quantity
1 - 1- 60 //The product is unable To purchase IT has greater quantity regarding The stock Table
1 - 2- 40 //I can purchase This product as
// This isn't available in stock Table but The following code blocks IT Too我正在使用一种表格,以提交要求的产品在购买表。因此,我需要的是匹配库存和库存表的个别库存的产品,如果它是少于最小的库存,然后可以进行购买。我试过但被困在了某个地方。有什么办法吗??
以下是我迄今尝试过的代码:
SMPURCHASEEntities dc = new SMPURCHASEEntities();
var con2 = (from s in dc.StoreInventoryDetails
select s).ToList();
string l = Session["EmployeeID"].ToString();
int g = Convert.ToInt32(l);
List<EmployeeDetails> con4 = (from r in dc.EmployeeDetails
where r.EmployeeID == (g)
select r).ToList();
List<Stock> con3 = (from f in dc.Stock
select f).ToList();
if (string.IsNullOrEmpty(txtQuantity.Text.Trim()))
{
lblMsg.Text = "Fields are empty";
return;
}
SMPURCHASEEntities context = new SMPURCHASEEntities();
int d = Convert.ToInt32(Session["EmployeeID"]);
List<EmployeeDetails> con = (from m in context.EmployeeDetails
where m.EmployeeID == (d)
select m).ToList();
List<DemandOrderLine> con_02 = (from c in context.DemandOrderLine
select c).ToList();
DemandOrderLine k = new DemandOrderLine();
foreach (StoreInventoryDetails s in con2)
{
foreach (Stock f in con3)
{
foreach (EmployeeDetails r in con4)
{
if (f.ItemID == s.ItemID && s.Quantity > f.Quantity && f.StoreID == r.StoreID && s.UniTypeID == f.UnitTypeID)
{
Label1.Text = "You have The minimum stock!!";
}
else
{
k.OrderID = Session["OrderID"].ToString();
k.CategoryID = Convert.ToInt32(ddlCategoryID.SelectedValue);
k.ItemID = Convert.ToInt32(ddlItemID.SelectedValue);
k.UnitTypeID = Convert.ToInt32(ddlUnitTypeID.SelectedValue);
k.Quantity = Convert.ToDouble(txtQuantity.Text);
k.Status = 0;
k.ApprovalStatus = Convert.ToBoolean(0);
k.StoreID = r.StoreID;
k.TotalQuantity = Convert.ToDouble(txtQuantity.Text);
k.Dissolved = 0;
try
{
context.DemandOrderLine.Add(k);
context.SaveChanges();
}
catch (Exception ex)
{
ex.ToString();
}
}
}
}
}发布于 2014-09-24 07:48:11
要检查业务规则(产品在库存表中的数量是否少于库存表中的数量),请使用LINQ查询,而不是手动迭代每个集合。下面是帮助您理解该方法的代码(工作示例):
public class Program
{
public static void Main(string[] args)
{
const int employeeId = 1;
var storeId = EmployeeList.Single(t => t.EmployeeId == employeeId).StoreId;
CanAddProduct(1, storeId); // false
CanAddProduct(2, storeId); // true
}
static bool CanAddProduct(int itemId, int storeId)
{
var inventory = InventoryList.FirstOrDefault(t => t.ItemId == itemId && t.StoreId == storeId);
if (inventory == null)
{
throw new ApplicationException("No such product in inventory");
}
var stock = StockList.Where(st => st.ItemId == itemId &&
st.Quantity < inventory.Quantity && st.StoreId == storeId);
if (stock.Any())
{
Console.WriteLine("You can't add ItemId={0}. You have The minimum stock", itemId);
return false;
}
Console.WriteLine("You can add ItemId={0}", itemId);
return true;
}
}https://stackoverflow.com/questions/26009043
复制相似问题