将现有列表与“更新”合并。名单

时间:2014-11-18 16:45:58

标签: c# list

我有两个清单: 1)现有清单 2)更新列表

两个列表都包含以下类

public class Subcategory
{
  public int category_id { get; set; }
  public List<ProductInfo> products { get; set; }
  public List<Subcategory> subcategories { get; set; }
}

清单1:

Subcat1
- Id: 1
- products:
  {product 1}
  {product 2}
- subcategories:
  {subcat2
   - Id: 2
   - products:
    {product 3}
   - subcategories:
    {subcat3
     - Id: 2
     - products:
      {product 4}
     - subcategories:
        {subcat4}
  }

列表2是一个更新列表,它包含新的类别和产品

Subcat1
- Id: 1
- products:
  {product 5}
- subcategories:
  {subcat2
   - Id: 2
   - products:
    {product 6}
   - subcategories:
    {subcat5
     - Id: 5
     - products:
      {product 7}
  }

那会发生什么: - 将产品5添加到Subcat1 - 将产品6添加到Subcat1 - 将subcat5添加到subcat2

的类别列表中

我想出了如何更新现有项目并删除删除项目。 我有一个int列表,其中包含所有类别的id和所有产品中的一个,如果当前产品或类别不在相应列表中,则将其删除。 递归地我还生成了List2中可以找到的所有类别和所有产品的列表,称为generatedCategories和generatedProducts。 然后,使用以下代码适用于此更新和删除行为:

private List<Product.Subcategory> UpdateObjects(List<Product.Subcategory> prod)
{
    try

    {
        int catcount = prod.Count;
        if (catcount > 0)
        {
            for (int i = catcount - 1; i > -1; i--)
            {
                try
                {
                    //Check if exists
                    if (categories.All(u => u.id != prod[i].category_id))
                    {
                        prod.RemoveAt(i);
                    }
                }
                catch (Exception)
                {

                    //throw;
                }

                foreach (var newsubCat in generatedSubcategories)
                {
                    if (prod[i].category_id == newsubCat.category_id)
                    {
                        prod[i].delivery_time = newsubCat.delivery_time;
                        prod[i].image = newsubCat.image;
                        prod[i].name = newsubCat.name;
                        prod[i].pickup_time = newsubCat.delivery_time;
                        prod[i].sequence = newsubCat.sequence;
                        prod[i].translation_missing = newsubCat.translation_missing;
                        prod[i].use_firm_time_settings = newsubCat.use_firm_time_settings;
                    }
                }
                if (prod[i].products != null)
                {
                    foreach (var oldProduct in prod[i].products)
                    {
                        int count = generatedProducts.Count;
                        if (count != 0)
                        {
                            for (int z = count - 1; i > -1; i--)
                            {
                                try
                                {
                                    //Check if exists
                                    if (products.All(u => u.id != generatedProducts[z].id))
                                    {
                                        generatedProducts.RemoveAt(z);
                                    }
                                    else
                                    {
                                        if (oldProduct.id == generatedProducts[z].id)
                                        {
                                            oldProduct.amount = generatedProducts[z].amount;
                                            oldProduct.comment = generatedProducts[z].comment;
                                            oldProduct.delivery_time = generatedProducts[z].delivery_time;
                                            oldProduct.description = generatedProducts[z].description;
                                            oldProduct.image = generatedProducts[z].image;
                                            oldProduct.ingredients = generatedProducts[z].ingredients;
                                            oldProduct.name = generatedProducts[z].name;
                                            oldProduct.pickup_time = generatedProducts[z].pickup_time;
                                            oldProduct.price = generatedProducts[z].price;
                                            oldProduct.sequence = generatedProducts[z].sequence;
                                            oldProduct.times = generatedProducts[z].times;
                                            oldProduct.translation_missing = generatedProducts[z].translation_missing;
                                            oldProduct.unit = generatedProducts[z].unit;
                                            oldProduct.use_category_time_settings = generatedProducts[z].use_category_time_settings;

                                        }
                                    }
                                }
                                catch (Exception)
                                {

                                    //throw;
                                }
                            }
                        }

                    }

                    if (prod[i].subcategories != null)
                    {
                        GenerateCategoryList(prod[i].subcategories);
                    }
                }
            }
        } 
    }
    catch (Exception)
    {
        //throw;
    }
    return prod;
}

我如何在指定位置添加新类别和产品?

1 个答案:

答案 0 :(得分:0)

好的,感谢Piyush的提示,我想出了一个粗略的方法(需要优化,但通常有效):

1)将原始列表存储在变量中(oldList从另一个类传递)

    Subcategory OriginalList = new Subcategory();
    OriginalList = oldList;

2)生成包含所有待更新类别和产品的字典 'prod'是基础树,你会递归地通过它。

我在Subcategory类中添加了一个名为parent_category_id的额外字段,它显然存储了父ID,每次迭代都会将其传递给函数。 稍后更新现有项目需要generatedProducts和generatedSubcategories。

    List<Product.ProductInfo> generatedProducts = new List<Product.ProductInfo>();
    List<Product.Subcategory> generatedSubcategories = new List<Product.Subcategory>();
    Dictionary<int, List<Product.Subcategory>> dicCategories = new Dictionary<int, List<Product.Subcategory>>();
    Dictionary<int, List<Product.ProductInfo>> dicProducts = new Dictionary<int, List<Product.ProductInfo>>();

    private List<Product.Subcategory> GenerateProductList(List<Product.Subcategory> prod, int parentCategoryId)
    {
        foreach (var subcategory in prod)
        {
            generatedSubcategories.Add(subcategory);
            if (dicCategories.ContainsKey(subcategory.category_id))
            {
                //Add to value
                subcategory.parent_category_id = parentCategoryId;
                dicCategories[subcategory.category_id].Add(subcategory);
            }
            else
            {
                subcategory.parent_category_id = parentCategoryId;
                dicCategories.Add(subcategory.category_id, new List<Product.Subcategory> { subcategory });
            }

            if (subcategory.products != null)
            {
                foreach (var product in subcategory.products)
                {
                    generatedProducts.Add(product);
                    if (dicProducts.ContainsKey(subcategory.category_id))
                    {
                        //Add to value
                        dicProducts[subcategory.category_id].Add(product);
                    }
                    else
                    {
                        dicProducts.Add(subcategory.category_id, new List<Product.ProductInfo>{ product });
                    }
                } 
            }
            if (subcategory.subcategories != null)
            {
                GenerateProductList(subcategory.subcategories, subcategory.category_id);
            }
        }
        return prod;
    }

3)调用GenerateProductList用新数据填充列表,使用父ID 0表示它是根。

GenerateProductList(yourNewDataList, 0);

4)好的,现在是时候创建一个包含新项目和旧项目的列表了。

    Product NewList = new Product();
    private void AddNewItems()
    {
        NewList.data = new List<Product.Subcategory>();
        foreach (var subcat in OriginalList.data)
        {
            if (!NewList.data.Any(c => c.category_id == subcat.category_id))
            {
                NewList.data.Add(subcat);
            }

        }

        Product tempList = Product.DeepCopy(NewList);


        foreach (var cat in dicCategories)
        {
            foreach (var toAdd in cat.Value)
            {
                if (toAdd.parent_category_id == 0)
                {
                    //Root
                    if (!NewList.data.Any(c => c.category_id == toAdd.category_id))
                    {
                        tempList.data.Add(toAdd);
                    }
                }
                else
                {

                    foreach (var subcat in OriginalList.data)
                    {
                        Product.Subcategory y = Product.FindSubCat(subcat, toAdd.parent_category_id);
                        if (y != null)
                        {
                            Product.Subcategory x = BrowseCategories(subcat, toAdd);
                            int index = NewList.data.IndexOf(subcat);
                            tempList.data[index] = x;
                        }
                    }
                }
            }
        }


        NewList = tempList;

        foreach (var product in dicProducts)
        {

            foreach (var subcat in NewList.data)
            {
                foreach (var subproduct in product.Value)
                {
                    Product.Subcategory y = Product.FindSubCat(subcat, product.Key);
                    if (y != null)
                    {
                        Product.Subcategory x = BrowseProducts(subcat, subproduct, product.Key);
                        int index = NewList.data.IndexOf(subcat);
                        tempList.data[index] = x;
                        x = x;
                    }

                    //tempList.data[subcat] =
                }
            }
        }
        NewList = tempList;
    }
    private Product.Subcategory BrowseProducts(Product.Subcategory category, Product.ProductInfo toAdd, int catId)
    {
        try
        {
            if (category.category_id == catId)
            {
                if (category.products == null) category.products = new List<Product.ProductInfo>();
                var remove = category.products.SingleOrDefault(s => s.id == toAdd.id);
                if (remove != null)
                {
                    if (remove.id != 0)
                    {
                        category.products.Remove(remove);
                    }
                }
                category.products.Add(toAdd);
                return category;
            }
            if (category.products == null) return category;
            if (category.subcategories == null) return category;
            foreach (var cat in category.subcategories)
            {
                if (cat.category_id == catId)
                {
                    if (cat.products == null) cat.products = new List<Product.ProductInfo>();
                    var remove = cat.products.SingleOrDefault(s => s.id == toAdd.id);
                    if (remove != null)
                    {
                        if (remove.id != 0)
                        {
                            cat.products.Remove(remove);
                        }
                    }
                    cat.products.Add(toAdd);
                    return category;
                }
                BrowseProducts(cat, toAdd, catId);

            }
            return category;
        }
        catch (Exception)
        {

            throw;
        }
    } 


    private Product.Subcategory BrowseCategories(Product.Subcategory category, Product.Subcategory toAdd)
    {
        try
        {
            if (category.category_id == toAdd.parent_category_id)
            {
                if (category.subcategories == null) category.subcategories = new List<Product.Subcategory>();
                var remove = category.subcategories.SingleOrDefault(s => s.category_id == toAdd.category_id);
                if (remove != null)
                {
                    if (remove.category_id != 0)
                    {
                        category.subcategories.Remove(remove);
                    }
                }
                category.subcategories.Add(toAdd);
                return category;
            }
            if (category.subcategories == null) return category;
            foreach (var cat in category.subcategories)
            {
                if (cat.category_id == toAdd.parent_category_id)
                {
                    if (cat.subcategories == null) cat.subcategories = new List<Product.Subcategory>();
                    var remove = cat.subcategories.SingleOrDefault(s => s.category_id == toAdd.category_id);
                    if (remove != null)
                    {
                        if (remove.category_id != 0)
                        {
                            cat.subcategories.Remove(remove);
                        }
                    }
                    cat.subcategories.Add(toAdd);
                    return category;
                }
                BrowseCategories(cat, toAdd);

            }
            return category;
        }
        catch (Exception)
        {

            throw;
        }

    } 

5)太棒了! NewList现在包含所有旧数据和新数据,但我们仍然需要更新旧数据。

    private List<Product.Subcategory> UpdateObjects(List<Product.Subcategory> prod)
    {
        try

        {
            int catcount = prod.Count;
            if (catcount > 0)
            {
                for (int i = catcount - 1; i > -1; i--)
                {
                    //if (prod[i].category_id == 203)
                    //{
                    //    Debug.WriteLine("");
                    //}
                    try
                    {
                        //Check if exists
                        if (categories.All(u => u.id != prod[i].category_id))
                        {
                            //if (prod[i].category_id == 203)
                            //{
                            //    prod.RemoveAt(i);
                            //}
                            if (!dicCategories.ContainsKey(prod[i].category_id))
                            {
                                prod.RemoveAt(i);

                            }
                        }
                    }
                    catch (Exception)
                    {

                        //throw;
                    }

                    foreach (var newsubCat in generatedSubcategories)
                    {
                        if (prod[i].category_id == newsubCat.category_id)
                        {
                            prod[i].delivery_time = newsubCat.delivery_time;
                            prod[i].image = newsubCat.image;
                            prod[i].name = newsubCat.name;
                            prod[i].pickup_time = newsubCat.delivery_time;
                            prod[i].sequence = newsubCat.sequence;
                            prod[i].translation_missing = newsubCat.translation_missing;
                            prod[i].use_firm_time_settings = newsubCat.use_firm_time_settings;
                        }
                    }
                    if (prod[i].products != null)
                    {
                        foreach (var oldProduct in prod[i].products)
                        {
                            int count = generatedProducts.Count;
                            if (count != 0)
                            {
                                for (int z = count - 1; i > -1; i--)
                                {
                                    try
                                    {
                                        //Check if exists
                                        if (products.All(u => u.id != generatedProducts[z].id))
                                        {
                                            generatedProducts.RemoveAt(z);
                                        }
                                        else
                                        {
                                            if (oldProduct.id == generatedProducts[z].id)
                                            {
                                                oldProduct.amount = generatedProducts[z].amount;
                                                oldProduct.comment = generatedProducts[z].comment;
                                                oldProduct.delivery_time = generatedProducts[z].delivery_time;
                                                oldProduct.description = generatedProducts[z].description;
                                                oldProduct.image = generatedProducts[z].image;
                                                oldProduct.ingredients = generatedProducts[z].ingredients;
                                                oldProduct.name = generatedProducts[z].name;
                                                oldProduct.pickup_time = generatedProducts[z].pickup_time;
                                                oldProduct.price = generatedProducts[z].price;
                                                oldProduct.sequence = generatedProducts[z].sequence;
                                                oldProduct.times = generatedProducts[z].times;
                                                oldProduct.translation_missing = generatedProducts[z].translation_missing;
                                                oldProduct.unit = generatedProducts[z].unit;
                                                oldProduct.use_category_time_settings = generatedProducts[z].use_category_time_settings;

                                            }
                                        }
                                    }
                                    catch (Exception)
                                    {

                                        //throw;
                                    }


                                }
                            }

                        }

                        if (prod[i].subcategories != null)
                        {
                            UpdateObjects(prod[i].subcategories);
                        }


                    }
                }
            } 
        }
        catch (Exception)
        {

            //throw;
        }

        return prod;
    }

调用该函数:

    List<Product.Subcategory> completedList = UpdateObjects(addedList.data);

现在completedList包含所有新的,旧的和更新的项目!

关于改善这一点的建议仍然受到欢迎。