LINQ to SQL - 按parentId对类别进行分组

时间:2010-05-25 13:25:32

标签: asp.net-mvc linq-to-sql asp.net-mvc-2

我正在尝试使用我的数据库中的Categories表构建导航菜单。

我在类别表中有类似的布局。

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分组)集合传递给我的视图。

怎么做?

3 个答案:

答案 0 :(得分: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 ) );
}

答案 1 :(得分:0)

看起来这是将模型转换为viewModel时会派上用场的一个很好的例子。你可以使用@thomas描述的相同技术创建一个CategoryViewModel的集合,它具有CategoryViewModel的Childrens属性。

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

答案 2 :(得分: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

然后,您可以在视图中循环显示它并相应地格式化。