0

购买后,我想管理数据库中的库存水平(以减少产品数量可用性)。并在数量少于 2 时提醒用户。或者有一个只读输入框来显示每个产品的可用数量。我的 Datacontext 包括 Products、Sales、Customers、Categories 和 SalesDetails

在 saleButton 中,我将每个产品添加到列表中,然后将数量和价格相乘,然后得到添加到列表中的产品价格的总和。每个购买列表都有一个 ID

然后在提交销售按钮中,我希望它处理付款并获取列表并保存在数据库中,然后从库存中扣除每件商品的数量。这就是问题所在。我正在尝试使用 LINQ 获取库存水平和总产品,但我遇到了错误。付款处理后如何更改库存水平?

谢谢

这是客户向产品列表中“添加项目”

    private void SalesButton_Click(object sender, RoutedEventArgs e)
        {
            SaleId += 1;
            var newDate = date.ToString("F");

            if (AddSalesProductNameComboBox.Text != "" && AddSalesCustomerNameComboBox.Text != "" && 
                ProductSalesPrice.Text != "" && ProductQuantityBought.Text != "")
            {

                var sale = new Sale
                {
                    SalesID = SaleId,
                    ProductName = AddSalesProductNameComboBox.Text,
                    CustomerName = AddSalesCustomerNameComboBox.Text,
                    ProductPrice = Convert.ToDecimal(ProductSalesPrice.Text),
                    QuantityPurchased = Convert.ToInt32(ProductQuantityBought.Text),
                    Date =   DateTime.Parse(newDate), 
                    Total = Convert.ToDecimal(ProductSalesPrice.Text) * Convert.ToInt32(ProductQuantityBought.Text)

                };

                posManager.AddSales(sale);

                var NewAmount = posManager.Sales.Sum(c => c.Total);
                AmountDueDisplay.Text = NewAmount.ToString();
            }
        }

这是向数据库提交销售并处理库存级别

 private void SubmitSalesButton_Click(object sender, RoutedEventArgs e)
        {

            decimal Balance = 0;
            //Check if inputs are empty
            if (AmountPaidInput.Text != "" && AmountDueDisplay.Text != "")
            {
                //Calculating the Amount due and amouunt paid
                decimal AmtDue = Convert.ToDecimal(AmountDueDisplay.Text);
                decimal AmtPaid = Convert.ToDecimal(AmountPaidInput.Text);
                DisplayBalance.Text = (AmtDue - AmtPaid).ToString();
            }

            //pass the calculated balance to the balance variable  
            Balance = Convert.ToDecimal(DisplayBalance.Text);

            //SaleDetailsID += 1;
            var newSalesDate = date.ToString("F");


            var salesDetails = new SalesDetail
            {
                SalesDetailsID = SaleDetailsID,
                SalesID = SaleId,
                SalesDetailsCustomerName = AddSalesCustomerNameComboBox.Text,
                SalesProductName = AddSalesProductNameComboBox.Text,
                AmountPaid = Convert.ToDecimal(AmountPaidInput.Text),
                Balance = Balance,
                Total = TotalQtyPurchased,
                SalesDetailsDate = Convert.ToDateTime(newSalesDate),
                //QuantityPuchased = Convert.ToInt32(ProductQuantityBought.Text)
                //QuantityPuchased = quan
                //SalesDetailsDate = 

            };

            posDBcontext.GetSalesDetails.Add(salesDetails);
            posDBcontext.SaveChanges();


            var q = SubmitSalesButton.Content as Product;
            var s = posDBcontext.Sales.Where(a => a.QuantityPurchased == q.QuantityAvailableInStock);


            AmountPaidInput.Clear();
            AmountDueDisplay.Clear();


            SalesDatagrid.ItemsSource = null;
            SalesDatagrid.Items.Clear();



            //var s = posDBContext.GetSalesDetails.Where(c => c.SalesID == c.SalesID);
            //var DetailSales = posManager.Sales.Where(c=> c.)

            //posManager.CreateSales(s);



            //TotalQtyPurchased = posDBContext.Sales.Sum(c => c.QuantityPurchased);
            //posDBContext.Products.

        }

我试着这样做

var TotalQtyBought = posDBcontext.Sales.Where(c => c.Total == c.Total); 
var StockLevel = posDBcontext.Products.Where(c => c.ProductPrice == c.ProductPrice); 

foreach (var item in StockLevel) { var newStock = item - TotalQtyBought; } 

我收到了这个错误

But I got this error cannot apply operands to type Product and iQueryable<sale> ```
4

1 回答 1

0

您关于 EF 的示例没有任何意义,因此我建议您阅读使用 EF 的基础知识以及如何:阅读实体、更新实体和插入实体。

想想 EF 实体和查询,例如使用表和 SQL。

如果您想查找产品并对其进行更新:

// -- SQL --
UPDATE Product SET Quantity = Quantity - 1 WHERE ProductId = 101;

// -- EF --
var product = context.Products.Single(x => x.ProductId == 101);
product.Quantity -= 1;
context.SaveChanges();

如果要插入新的销售记录。

// -- SQL --
INSERT INTO Sales (ProductId, Total) 
SELECT ProductId, Price FROM Products WHERE ProductId = 101;

// -- EF --
var product = context.Products.Single(x => x.ProductId == 101);
var sale = new Sale { Product = product, Total = product.Price };
context.Sales.Add(sale);
context.SaveChanges();

因此,在您了解 EF 如何选择数据、(WhereSingleToList等)实体如何相互关联数据以及如何插入和更新实体以创建和更新相关数据之前,回答您的问题将很困难表。一旦你掌握了要点,那么你应该更进一步,并能够用一个更好的例子来重新表述你的问题,说明你已经尝试过什么,以及你想要完成什么。

于 2019-05-01T22:03:17.637 回答