2012-12-12 137 views
1

我有一個產品列表(​​),其中所有數據都預填充。填充嵌套列表中的嵌套列表

型號: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將是​​型的,意味着它是嵌套的,它可以再次包含以下兒童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)。這是可能的遞歸方式?

+0

當你「遞歸可能的方式:」你的意思是你要每個孩子-產品的新副本寫的嗎?或者使用相同的參考是好的? –

回答

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; 
} 
+0

謝謝你的工作。 – HashCoder