如何从没有特定值的数据库中删除值

时间:2015-08-20 08:28:52

标签: c# wpf linq wcf

上面的标题是我可以尝试提问的最佳方式,如果在您阅读下面的问题之后没有正确标题,那就很抱歉。 :)

所以,我在下面编码,我从我从另一个表中获取的信息中向我的数据表中的某些列添加数据。它几乎就像Excel中的VLOOKUP。

    var qisg = new QuoteItemSectionGroup
    {
        SectionGroup = db.SectionGroups.Where(x => x.Name == "Ali Bottom Rail" && x.Section == TruckSection.FrontEndRequirments).First(),
        //StockItem is set here
        StockItem = quoteItem.BodyType.Name == "Royal Corrugated" || quoteItem.BodyType.Name == "Royal Smooth Riveted" ? db.StockItems.Where(x => x.StockCode == "AEX165").First() : null,
    };
    qisg.Quantity = qisg.StockItem == null ? 0 : 1;
    qisg.Weight = Math.Round(((double)qisg.Length * (double)qisg.Quantity) * (double)qisg.StockItem.Mass, 2);
    qisg.Cost = Math.Round(((double)qisg.Length * (double)qisg.Quantity) * (double)qisg.StockItem.UnitCost, 2);
    quoteItem.SectionGroups.Add(qisg);

你可以看到我设置StockItem的位置,我说:如果 BodyType = Royal Corrugated那么StockItem代码应该是AEX165 其他股票代码应为null。我从 StockCode 获得所有所需的数量,重量和费用信息,就像VLOOKUP一样。

现在我的主要问题是,当我在StockItem中将其他设置为 null 时,我将无法从我的SectionGroup获取任何信息数据表,但我手动设置的var qisg除外。因此,当StockItem为null时,我希望能够跳过整个var qisg,而不是将数据添加到我的数据表中。

如果我无法跳过 foreach (var sectionGroup in quote.Items.First().SectionGroups) bills.Add(new ListOfBills { Cost = sectionGroup.Cost, PricePerMeter = sectionGroup.Price_m, GroupName = sectionGroup.SectionGroup.Name, Length = sectionGroup.Length, Quantity = sectionGroup.Quantity, StockCode = sectionGroup.StockItem.StockCode, StockDescription = sectionGroup.StockItem.Description, TruckSection = sectionGroup.SectionGroup.Section, Weight = sectionGroup.Weight, Width = sectionGroup.Width }); return bills; ,则会抛出以下错误:

  

对象引用未设置为对象的实例。

在下面的编码中:

StockItem

所以如果public void insertTextIntoInput(Integer inputId, String text) { onView(withId(inputId)).perform(typeText(text), closeSoftKeyboard()); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } null ,我真的需要知道如何跳过变量。任何帮助或建议真的很棒!很抱歉,我不知道某些对象和类型的所有正确技术术语是c#,所以请仔细阅读它,如果您需要更多信息,请不要犹豫。 :)

2 个答案:

答案 0 :(得分:1)

如果我理解正确,只有当 StockItem 不为空时,才需要将新的 qisg 添加到sectionGroup。尝试更改您的代码,如下所示:

var stockItem = quoteItem.BodyType.Name == "Royal Corrugated" || 
                quoteItem.BodyType.Name == "Royal Smooth Riveted" 
                    ? db.StockItems.Where(x => x.StockCode == "AEX165").First() 
                    : null;

if(stockItem != null)
{
   var qisg = new QuoteItemSectionGroup
   {
      SectionGroup = db.SectionGroups.Where(x => x.Name == "Ali Bottom Rail" && x.Section == TruckSection.FrontEndRequirments).First(),
      StockItem = stockItem,
   };

   qisg.Quantity = 1;
   qisg.Weight = Math.Round(((double)qisg.Length * (double)qisg.Quantity) * (double)qisg.StockItem.Mass, 2);
   qisg.Cost = Math.Round(((double)qisg.Length * (double)qisg.Quantity) * (double)qisg.StockItem.UnitCost, 2);

   quoteItem.SectionGroups.Add(qisg);
}

答案 1 :(得分:1)

我想我不明白你的问题。 看来你只需要分解治疗。

var myStockItem = quoteItem.BodyType.Name == "Royal Corrugated" || quoteItem.BodyType.Name == "Royal Smooth Riveted" ? db.StockItems.Where(x => x.StockCode == "AEX165").First() : null;

if(myStockItem != null)
{
    var qisg = new QuoteItemSectionGroup
    {
        SectionGroup = db.SectionGroups.Where(x => x.Name == "Ali Bottom Rail" && x.Section == TruckSection.FrontEndRequirments).First(),
        //StockItem is set here
        StockItem = myStockItem,
    };
    qisg.Quantity = qisg.StockItem == null ? 0 : 1;
    qisg.Weight = Math.Round(((double)qisg.Length * (double)qisg.Quantity) * (double)qisg.StockItem.Mass, 2);
    qisg.Cost = Math.Round(((double)qisg.Length * (double)qisg.Quantity) * (double)qisg.StockItem.UnitCost, 2);
    quoteItem.SectionGroups.Add(qisg);
}

这是你需要的......?