Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从树结构中获取列表_C# - Fatal编程技术网

C# 从树结构中获取列表

C# 从树结构中获取列表,c#,C#,我在c#中有一个类CategoryModel,它是树的一个元素: public class CategoryModel { public string Id { get; set; } public string Name { get; set; } public string NameEng { get; set; } public string ParentCategoryId { get; set; } public ICollection<s

我在c#中有一个类CategoryModel,它是树的一个元素:

public class CategoryModel
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string NameEng { get; set; }
    public string ParentCategoryId { get; set; }
    public ICollection<string> ChildCategoriesIds { get; set; } = new List<string>();
    public ICollection<string> ProductsIds { get; set; } = new List<string>();
}

public class Product
{
    public string Id { get; set; }
    public string Name { get; set; }
    public string NameEng { get; set; }
}

您可以为
CategoryNew
指定自己的构造函数,该构造函数将作为类
CategoryModel
的参数对象,其中您将基于
CategoryModel
的属性值设置
CategoryNew
的所有属性:

public CategoryNew(CategoryModel cm){
    // set properties
}
那么你的方法是:

public List<CategoryNew> ConverModelToNew(List<CategoryModel> lstCatModel){
    List<CategoryNew> lstCatNew = new List<CategoryNew>();

    foreach(var item in lstCatModel){
        lstCatNew.Add(new CetagoryNew(item));
    }

    return lstCatNew;
}
公共列表ConverModelNew(列表lstCatModel){
List lstCatNew=新列表();
foreach(lstCatModel中的var项){
lstCatNew.Add(新的CetagoryNew(项目));
}
返回新的;
}

假设您正在尝试将一组对象转换为另一组对象。 首先,我认为类别应该继承
UidName
,这样可以通过减少重复对象来提高内存效率

public class CategoryNew: UidName
{
    public IEnumerable<CategoryNew> ChildCategories { get; set; } = new List<CategoryNew>();
    public IEnumerable<UidName> Products { get; set; } = new List<UidName>();
}

public class UidName
{
    public string Uid { get; set; }
    public string Name { get; set; }
    public string NameEng { get; set; }
    public bool? IsDeleted { get; set; }
}

// first run to create products
var newProducts = products.Select(p => new UidName {
    Uid = p.Id,
    Name = p.Name,
    NameEnd = p.NameEng
}).ToArray();

// second run to create categories with products
var newCategories = categories.Select(c => new CategoryNew {
    Uid = c.Id,
    Name = c.Name,
    NameEng = c.NameEng,
    IsDeleted = (bool?)null,  //TODO
    Products = newProducts.Where(p => c.ProductIds.Contains(p.Uid))
                          .ToList()
}).ToArray();

// last run find sub categories
foreach(var category in newCategories) {
    var oldCategory = categories.First(c => c.Id == category.Uid);
    category.ChildCategories = newCategories.Where(c => oldCategory.ChildCategoriesIds.Contains(c.Uid))
                                            .ToArray();
}
public类CategoryNew:UidName
{
public IEnumerable ChildCategories{get;set;}=new List();
公共IEnumerable产品{get;set;}=new List();
}
公共类UidName
{
公共字符串Uid{get;set;}
公共字符串名称{get;set;}
公共字符串NameEng{get;set;}
公共布尔?被删除{get;set;}
}
//第一次运行以创建产品
var newProducts=products.Select(p=>newuidname{
Uid=p.Id,
名称=p.名称,
NameEnd=p.NameEng
}).ToArray();
//第二次运行以使用产品创建类别
var newCategories=categories.Select(c=>newcategorynew{
Uid=c.Id,
Name=c.Name,
NameEng=c.NameEng,
IsDeleted=(bool?)null,//TODO
Products=newProducts.Where(p=>c.ProductIds.Contains(p.Uid))
托利斯先生()
}).ToArray();
//上次运行查找子类别
foreach(新类别中的var类别){
var oldCategory=categories.First(c=>c.Id==category.Uid);
category.ChildCategories=newCategories.Where(c=>oldCategory.childCategoriesId.Contains(c.Uid))
.ToArray();
}

您的问题是什么?将列表转换为列表的方法将列表转换为包含子类别和产品的列表的方法
public class CategoryNew: UidName
{
    public IEnumerable<CategoryNew> ChildCategories { get; set; } = new List<CategoryNew>();
    public IEnumerable<UidName> Products { get; set; } = new List<UidName>();
}

public class UidName
{
    public string Uid { get; set; }
    public string Name { get; set; }
    public string NameEng { get; set; }
    public bool? IsDeleted { get; set; }
}

// first run to create products
var newProducts = products.Select(p => new UidName {
    Uid = p.Id,
    Name = p.Name,
    NameEnd = p.NameEng
}).ToArray();

// second run to create categories with products
var newCategories = categories.Select(c => new CategoryNew {
    Uid = c.Id,
    Name = c.Name,
    NameEng = c.NameEng,
    IsDeleted = (bool?)null,  //TODO
    Products = newProducts.Where(p => c.ProductIds.Contains(p.Uid))
                          .ToList()
}).ToArray();

// last run find sub categories
foreach(var category in newCategories) {
    var oldCategory = categories.First(c => c.Id == category.Uid);
    category.ChildCategories = newCategories.Where(c => oldCategory.ChildCategoriesIds.Contains(c.Uid))
                                            .ToArray();
}