2010-05-25 80 views
0

我想從我的數據庫使用類別表構造一個導航菜單。LINQ to SQL - 由parentId分組類別

我在類別表中具有類似的佈局,如下所示。

public List<Category> CategoryData = new List(new Category[] { 
             new Category{ CategoryId = 1, Name = "Fruit", ParentCategoryId = null}, 
             new Category{ CategoryId = 2, Name = "Vegetables", ParentCategoryId = null}, 
             new Category{ CategoryId = 3, Name = "Apples", ParentCategoryId = 1}, 
             new Category{ CategoryId = 4, Name = "Bananas", ParentCategoryId = 1}, 
             new Category{ CategoryId = 5, Name = "Cucumber", ParentCategoryId = 2}, 
             new Category{ CategoryId = 6, Name = "Onions", ParentCategoryId = 2} 
           ); } 

上面應該返回類似

水果(父)

"===Apples, Bananas (child) 

蔬菜(父)

"===Cucumber, Onions (child) 

我需要能夠通過這是某種'分組'(按parentid分組)到我的視圖。

如何做到這一點?

回答

0

這似乎像這樣的轉換你的模型到視圖模型會派上用場時,一個很好的例子。正如您可以使用@thomas描述的相同技術創建具有CategoryViewModel的Childrens屬性的CategoryViewModel的集合。

public class CategoryViewModel 
{ 
    public int CategoryId { set; get; } 
    public string CategoryName { set; get; } 
    public int? ParentCategoryId { set; get; } 
    public IEnumerable<CategoryViewModel> Children { set; set; } 
} 

public static IEnumerable<CategoryViewModel> GetAllCategoryViewModel(IList<Category> categories) 
{ 
     var query = GetChildren(null, categories); 
     return query.ToList(); 

} 

public static IEnumerable<CategoryViewModel> GetChildren(int? parentId, IList<Category> categories) 
{ 
    var children = from category in categories 
        where category.ParentCategoryId == parentId 
        select 
        new CategoryViewModel 
        { 
         CategoryId = category.CategoryId, 
         CategoryName = category.CategoryName, 
         ParentCategoryId = category.ParentCategoryId, 
         Children = GetChildren(category.CategoryId, categories) 
        }; 

    return children; 
} 
0

如何像:

private void Test() 
{ 
    var categoryData = new List 
             { 
              new Category {CategoryId = 1, Name = "Fruit", ParentCategoryId = null}, 
              new Category {CategoryId = 2, Name = "Vegetables", ParentCategoryId = null}, 
              new Category {CategoryId = 3, Name = "Apples", ParentCategoryId = 1}, 
              new Category {CategoryId = 4, Name = "Bananas", ParentCategoryId = 1}, 
              new Category {CategoryId = 5, Name = "Cucumber", ParentCategoryId = 2}, 
              new Category {CategoryId = 6, Name = "Onions", ParentCategoryId = 2} 
             }; 
    var query = from category in categoryData 
       where category.ParentCategoryId == null 
       select category; 

    foreach (var item in query) 
    { 
     Debug.WriteLine(string.Format("{0} (parent)", item.Name)); 
     Debug.WriteLine(GetChildren(item.CategoryId, categoryData )); 
    } 

} 
private static string GetChildren(int parentCategoryId, IEnumerable categoryData) 
{ 
    var children = (from category in categoryData 
        where category.ParentCategoryId == parentCategoryId 
        select category.Name).ToArray(); 

    return string.Format("==={0} (child)", string.Join(", ", children)); 
}
0
var list = from a in CategoryData 
     join b in CategoryData on a.ParentCategoryId equals b.CategoryId into c 
     from d in c.DefaultIfEmpty() 
     where d != null 
     select new { 
      a.CategoryId, 
      a.Name, 
      a.ParentCategoryId, 
      ParentName = d.Name 
     }; 

回報

CategoryId Name  ParentCategoryId ParentName 
3    Apples  1     Fruit 
4    Bananas  1     Fruit 
5    Cucumber 2     Vegetables 
6    Onions  2     Vegetables 

然後,您可以通過它在循環相應的視圖和格式。