IEnumerable<T> Merge

本文关键字:Merge gt IEnumerable lt | 更新日期: 2023-09-27 18:34:17

我需要根据其categoryId合并IEnumerable<category>并列出其所有相关的子类别。从SQL中,我把所有东西都放在一起。

这就是我现在拥有的:

型:

public class Category
{
        public int CategoryId { get; set; }
        public string CategoryName { get; set; }
        public int SubCategoryId { get; set; }
        public string SubCategoryName { get; set; }        
}

结果:

<Category>
   <CategoryId>1</CategoryId>
   <CategoryName>Shoes</CategoryName>
   <SubCategoryId>2</SubCategoryId>
   <SubCategoryName>Baby Shoes</SubCategoryName>
</Category>
<Category>
   <CategoryId>1</CategoryId>
   <CategoryName>Shoes</CategoryName>    
   <SubCategoryId>4</SubCategoryId>
   <SubCategoryName>Man Shoes</SubCategoryName>
</Category>

我需要合并它并在IEnumerable<SubCategory>里面时返回IEnumerable<category>

型:

public class Category
{
        public int CategoryId { get; set; }
        public string CategoryName { get; set; }         
        public IEnumerable<SubCategory> SubCategory { get; set; }
}
public class SubCategory
{       
        public int SubCategoryId { get; set; }
        public string SubCategoryName { get; set; }               
}

结果:

<Category>
   <CategoryId>1</CategoryId>
   <CategoryName>Shoes</CategoryName>       
   <SubCategory>
        <SubCategoryId>2</SubCategoryId>
        <SubCategoryName>Baby Shoes</SubCategoryName>
   </SubCategory>
   <SubCategory>
        <SubCategoryId>2</SubCategoryId>
        <SubCategoryName>Baby Shoes</SubCategoryName>
   </SubCategory>
</Category>

可能吗?谢谢。

IEnumerable<T> Merge

我把你的第一个模型命名为OldCategory。查询:

        var categories = new OldCategory[]
        {
            new OldCategory {CategoryId = 1, SubCategoryId = 2},
            new OldCategory {CategoryId = 1, SubCategoryId = 4}
        };
        var newCategories = categories
            .GroupBy(_ => new
            {
                Id = _.CategoryId, 
                Name = _.CategoryName
            })
            .Select(_ => new Category
            {
                CategoryId = _.Key.Id, 
                CategoryName = _.Key.Name, 
                SubCategories = _.Select(sc => new SubCategory {SubCategoryId = sc.SubCategoryId, SubCategoryName = sc.SubCategoryName})
            })
            .ToArray();

DotNetFiddle:- https://dotnetfiddle.net/l0qsiB

public class Category
{
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }
    public int SubCategoryId { get; set; }
    public string SubCategoryName { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        List<Category> lstCategory = new List<Category>()
        {
            new Category(){CategoryId = 1, CategoryName = "Shoes", SubCategoryId = 2, SubCategoryName = "Baby Shoes"},
            new Category(){CategoryId = 1, CategoryName = "Shoes", SubCategoryId = 4, SubCategoryName = "Man Shoes"}
        };
        var grouped = lstCategory.GroupBy(obj => new { obj.CategoryId, obj.CategoryName}, (key,group)=> new {CategoryId=key.CategoryId,CategoryName=key.CategoryName,SubCategory = group});
    }
}