两个列表类别和子类别

时间:2017-05-31 08:03:50

标签: c# list linq

我有以下2个列表

- `List<Category>`
- `List<SubCategory>`

        public class Category
        {
            public string CategoryId 
           {
               get;
               set;
           }

           public List<SubCategory> subCategories
           {
               get;
               set;
           }


    public string Name
{
get;
set;
}
        }


        public class SubCategory
        {
             public string SubCategoryId
             {
                 get;
                 set;
             }
            public string CategoryId 
            {
                 get;
                 set;
            }
            public string Name
            {
                 get;
                 set;
            }
         }
  1. 我想在类别和子类别中找到匹配的类别(按CategoryId)。

  2. 将具有特定CategoryId的所有子类别添加到Category的subCategories数组中,并使用相同的CategoryId CategoryId。

  3. 到目前为止,我已经尝试过了。

    var categories = Categories.Select(c => c.Id);
    
                foreach(Guid categoryId in categories)
                {
                    var subCategoriesByCategoryId = validatedSubCategories.Where(subCats => subCats.CategoryId == categoryId);
                    List<SubCategory> subCategories = new List<SubCategory>();
                    foreach(SubCategory subCategory in subCategoriesByCategoryId)
                    {
                        subCategories.Add(subCategory);
                    }
    
                    if (subCategories.Count() > 0)
                    {
    var categoryById = Categories.FirstOrDefault(vC => vC.Id == categoryId);
    if (categoryById != null) categoryById.SubCategories = subCategories;
                    }
                }
    

    我如何实现这一目标?

    样本数据

    Category cat1= new Category {CategoryId=1};
    Category cat2= new Category {CategoryId=2};
    Category cat3= new Category {CategoryId=3};
    Category cat4= new Category {CategoryId=4};
    
    List<Category > Categories = new List<Category >()
    Categories.Add(cat1);
    Categories.Add(cat2);
    Categories.Add(cat3);
    Categories.Add(cat4);
    
    SubCategory sc1 = new SubCategory {CategoryId=1);
    SubCategory sc2 = new SubCategory{CategoryId=2};
    SubCategory sc3 = new SubCategory{CategoryId=3};
    SubCategory sc4 = new SubCategory{CategoryId=4};
    
    List<SubCategory> SubCategories = new List<SubCategory >()
    SubCategories.Add(sc1);
    SubCategories.Add(sc1);
    SubCategories.Add(sc3);
    SubCategories.Add(sc4);
    

    预期outPut

    2具有CategoryId 1的子类别中的子类别项目应添加到CategoryList中具有CategoryId 1的类别项目

    1具有CategoryId 3的子类别中的子类别项目应添加到CategoryList中具有CategoryId 3的类别项目

    1具有CategoryId 4的子类别中的子类别项目应添加到CategoryList中具有CategoryId 4的类别项目

1 个答案:

答案 0 :(得分:1)

请尝试以下声明:

List<Category> categories = new List<Category>();
List<SubCategory> subCategories = new List<SubCategory>();

var matchingCategories = from mt in categories
    join sub in subCategories
        on mt.CategoryId equals sub.CategoryId
    group sub by new { mt.CategoryId, mt.Name}
    into grp
    select new Category() {CategoryId = grp.Key.CategoryId , Name = grp.Key.Name, subCategories = grp.ToList()};

var result = matchingCategories.ToList();