【问题标题】:Populate a nested list from a nested list从嵌套列表填充嵌套列表
【发布时间】:2012-12-12 17:01:41
【问题描述】:

我有一个产品列表 (IList<Product>),其中预先填充了所有数据。

型号:Product

public class Product
{

    public int Id { get; set; }

    public int ParentProductId { get; set; }

    public string Code { get; set; }

    public string Name { get; set; }

    public IList<Product> Children { get; set; } 

}

属性Children 将是IList&lt;Product&gt; 类型,意味着它是嵌套的,它可以再次包含高达n 级别的子级。

我有另一个模特FilterModel

型号:FilterModel

public class FilterModel
{
    //// This is same as that of Product Code
    public string Code { get; set; }

    //// This is a combination of Products Name, Id and some static strings
    public string FilterUrl { get; set; }

    public IList<FilterModel> Children
}

也有同样的嵌套结构。

我计划将数据从第一个模型 (Product) 插入我的第二个模型 (FilterModel)。这可能以递归方式吗?

【问题讨论】:

  • 当您写“可能以递归方式”时,您的意思是您想要每个子产品的新副本?或者使用相同的参考可以吗?

标签: c# .net linq c#-4.0 nested


【解决方案1】:

试试这个:

FilterModel Transfer(Product product)
{
    var fm = new FilterModel();
    fm.Code = product.Code;
    fm.Children = new List<FilterModel>();

    foreach (var p in product.Children)
    {
        fm.Children.Add(Transfer(p));
    }

    return fm;
}

【讨论】:

    猜你喜欢
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    • 2019-01-01
    • 1970-01-01
    • 2020-12-10
    相关资源
    最近更新 更多