1

I have a list of Product (IList<Product>), with all data pre-populated.

Model: 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; } 

}

Property Children will be of type IList<Product>, means it is nested, it can again contain children up to n levels.

I have another model FilterModel.

Model: 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
}

Which also has a same nested structure.

I planning to insert data into my second model (FilterModel) from the first (Product). Is this possible in a recursive way?

1
  • When you wrote "possible in a recursive way" you mean you want a new copy of every Child-Product? Or using the same reference is OK? Commented Dec 12, 2012 at 17:16

1 Answer 1

1

try this:

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.