2016-09-01 6 views
-3

として、私はモデルの定義を持っている:私はちょうど最初のレベルのみを取得し、このリストに値を追加する必要がありますが、次のコードを使用してmvc 4のリストに値を追加するには?

a)Redbook 
    a.1) Wedding 
      a.1.1) Christian 
       . 
       . 
    a.2)Baptism 
b)Florenta 

のようなサブ項目のn個があるかもしれません

public class CMBCategory 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public List<CMBCategory> children = new List<CMBCategory>(); 
} 

サブ製品の:ここで

foreach (var items in lstcatlist) 
{ 
    CMBCategory cmb = new CMBCategory(); 
    cmb.ID = items.FirstOrDefault().catid; 
    cmb.Name = items.FirstOrDefault().title; 
    foreach (var item1 in items) 
    { 
     if (item1.parent == 0) 
     { 
      cmb.children.Add(new CMBCategory() { ID = item1.prod_id, Name = item1.desc }); 
     } 
    } 
    lstmain.Add(cmb); 
} 

lstcatlistは値があります。 enter image description here任意の

var lstcatlist = (from s in context.M_ProductCategory 
        join p in context.M_CategoryDetails on s.ID equals p.CategoryID 
        select new 
        { 
         catid = s.ID, 
         title = s.Title, 
         prod_id = p.ID, 
         parent = p.ParentID, 
         desc = p.Description 
        }).GroupBy(x => x.catid).ToList(); 
+0

を使用する必要がある場合これは、理由は私のループ構造のため、ループn個のサブ製品の数に正しい構造を見つける方法を提供してくださいすることができます'lstcatlist'にあり、そのモデルは何ですか? –

+0

@Stephen Muecke List lstmain = newリスト(); – neethu

+0

No - again - 'lstcatlist'には何があり、そのモデルは何ですか? –

答えて

0

は、あなたがデータで何再帰

void main() 
{ 
    PrintTree(listOfCategorys); 
} 

void PrintTree(IEnumerable<CMBCategory> list) 
{ 

    foreach(var item in list) 
    { 
    PrintTree(item.children); 
    DoSomehtingWithTheObject(item); // ie. print it , or what ever you want 

    } 
} 
関連する問題