如何从C#中的嵌套observable集合中删除子类?

时间:2017-11-03 06:46:27

标签: c#

我有一个发票的嵌套observable集合,我目前正在使用底部的代码从网格控件中删除选定的行。发票的一个例子是:

  ----------------------------------------------------
  | Item Name | Quantity | Unit Price |  Total Price  |
  ----------------------------------------------------
- |  Coffee   |     1    |    1.00    |     1.00      |
     -------------------------------------------------
    | Item Name | Quantity | Unit Price | Total Price |
     -------------------------------------------------
    |  Creamer  |    1    |    1.00    |     1.00     |  
    |   Sugar   |    1    |    1.00    |     1.00     | 
  ----------------------------------------------------
- |   Bread   |     1    |    1.00    |     1.00      |
     -------------------------------------------------
    | Item Name | Quantity | Unit Price | Total Price |
     -------------------------------------------------
    |   Cheese  |    1     |    1.00    |    1.00     | 
  ----------------------------------------------------
+ |   Water   |     1    |    1.00    |     1.00      | 
  ----------------------------------------------------

如果我选择CoffeeBreadWater,则会成功删除该项目及其子项目。

但如果我选择CreamerSugarCheese,则会删除父项,从而删除它拥有的每个子项。

我的代码是:

int rowUID = Convert.ToInt32(invoiceGC.GetFocusedRowCellValue("InvoiceItemUID"));

for (int i=0; i < Invoice.Count(); i++)
{
    if(Invoice[i].InvoiceItemUID == rowUID)
    {
        Invoice.Remove(Invoice[i]);
        break;
    }
    else
    {
        bool subDelete = false;
        for (int j=0; j < Invoice[i].InvoiceItemSub.Count(); j++)
        {
            if (Invoice[i].InvoiceItemSub[j].InvoiceItemUID == rowUID)
            {
                Invoice[i].InvoiceItemSub.Remove(Invoice[i].InvoiceItemSub[j]);
                subDelete = true;
                break;
            }
        }
        if(subDelete == true)
        {
            break;
        }
    }
}

我的课程是:

public class InvoiceItemMainClass
{
    private string invoiceItemMainName;
    private decimal invoiceItemMainQuantity;
    private decimal invoiceItemMainUnitPrice;
    private decimal invoiceItemMainTotalPrice;
    private int invoiceItemID;
    private int invoiceItemUID;
    private ObservableCollection<InvoiceItemSubClass> invoiceItemSub;
    public InvoiceItemMainClass(string invoiceItemMainName, decimal invoiceItemMainQuantity, decimal invoiceItemMainUnitPrice, decimal invoiceItemMainTotalPrice, int invoiceItemID, int invoiceItemUID)
    {
        this.invoiceItemMainName = invoiceItemMainName;
        this.invoiceItemMainQuantity = invoiceItemMainQuantity;
        this.invoiceItemMainUnitPrice = invoiceItemMainUnitPrice;
        this.invoiceItemMainTotalPrice = invoiceItemMainTotalPrice;
        this.invoiceItemID = invoiceItemID;
        this.invoiceItemUID = invoiceItemUID;
        this.invoiceItemSub = new ObservableCollection<InvoiceItemSubClass>();
    }
    public string InvoiceItemMainName { get { return invoiceItemMainName; } set { invoiceItemMainName = value; } }
    public decimal InvoiceItemMainQuantity { get { return invoiceItemMainQuantity; } set { invoiceItemMainQuantity = value; } }
    public decimal InvoiceItemMainUnitPrice { get { return invoiceItemMainUnitPrice; } set { invoiceItemMainUnitPrice = value; } }
    public decimal InvoiceItemMainTotalPrice { get { return invoiceItemMainTotalPrice; } set { invoiceItemMainTotalPrice = value; } }
    public int InvoiceItemID { get { return invoiceItemID; } set { invoiceItemID = value; } }
    public int InvoiceItemUID { get { return invoiceItemUID; } set { invoiceItemUID = value; } }
    public ObservableCollection<InvoiceItemSubClass> InvoiceItemSub { get { return invoiceItemSub; } }
}

public class InvoiceItemSubClass
{
    private string invoiceItemSubName;
    private decimal invoiceItemSubQuantity;
    private decimal invoiceItemSubUnitPrice;
    private decimal invoiceItemSubTotalPrice;
    private int invoiceItemID;
    private int invoiceItemUID;
    public InvoiceItemSubClass(string invoiceItemSubName, decimal invoiceItemSubQuantity, decimal invoiceItemSubUnitPrice, decimal invoiceItemSubTotalPrice, int invoiceItemID, int invoiceItemUID)
    {
        this.invoiceItemSubName = invoiceItemSubName;
        this.invoiceItemSubQuantity = invoiceItemSubQuantity;
        this.invoiceItemSubUnitPrice = invoiceItemSubUnitPrice;
        this.invoiceItemSubTotalPrice = invoiceItemSubTotalPrice;
        this.invoiceItemID = invoiceItemID;
        this.invoiceItemUID = invoiceItemUID;
    }
    public string InvoiceItemSubName { get { return invoiceItemSubName; } set { invoiceItemSubName = value; } }
    public decimal InvoiceItemSubQuantity { get { return invoiceItemSubQuantity; } set { invoiceItemSubQuantity = value; } }
    public decimal InvoiceItemSubUnitPrice { get { return invoiceItemSubUnitPrice; } set { invoiceItemSubUnitPrice = value; } }
    public decimal InvoiceItemSubTotalPrice { get { return invoiceItemSubTotalPrice; } set { invoiceItemSubTotalPrice = value; } }
    public int InvoiceItemID { get { return invoiceItemID; } set { invoiceItemID = value; } }
    public int InvoiceItemUID { get { return invoiceItemUID; } set { invoiceItemUID = value; } }
}

1 个答案:

答案 0 :(得分:0)

根据您的删除逻辑,它将始终删除Parent 考虑你有父项是&#34;咖啡&#34; Id = 1,它有SubItem&#34; Sugar&#34; Id = 1现在当你选择&#34; Sugar&#34; (这是SubItem,Id = 1)并循环遍历项目它与父项目#34;咖啡&#34;并删除你的循环中断。

要实现SubItem的删除从您的父项中,您需要提供两个参数/参数1)使用不同方法的Parent Id和SubItem Id,然后您可以从Parent的SubItem Collection中删除SubItem。