2017-07-06 47 views
0

我正在尋找一種有效的方法來計算給定類別的所有子類別(所有級別)的總數,而無需從數據庫中檢索所有數據計算自我參照表中所有後代總數的最有效方法

這裏是我的模型:

public class Category 
{ 
    public Category() 
    { 
     SubCategories = new HashSet<Category>(); 
    } 

    public Guid CategoryId { get; set; } 
    public string Title { get; set; } 

    [ForeignKey("ParentCategory")] 
    public Guid? ParentCategoryId { get; set; } 

    //navigation properties 
    public virtual Category ParentCategory { get; set; } 
    public virtual ICollection<Category> SubCategories { get; set; } 
} 

我的視圖模型:

public class CategoryViewModel 
{ 
    public CategoryViewModel() 
    { 
     SubCategories = new List<CategoryViewModel>(); 
    } 

    public Guid CategoryId { get; set; } 

    [Required] 
    public string Title { get; set; } 

    public Guid? ParentCategoryId { get; set; } 

    public virtual CategoryViewModel ParentCategory { get; set; } 

    public virtual List<CategoryViewModel> SubCategories { get; set; } 

    public int TotalOfDirectSubCategories { get; set; } 
    public int TotalOfAllSubCategories { get; set; } 
} 

我的自動映射:

  cfg.CreateMap<Category, CategoryViewModel>() 
      .PreserveReferences()     
      .ReverseMap(); 

最後我EF檢索數據:

   data = _ctx.Categories.Where(x => x.ParentCategoryId == categoryId) 
       //.Include(x => x.SubCategories) 
       .ToList(); 

假設某個類別可能包含數千個子類別。由於這將是SOA解決方案,並且數據將從Web API傳遞到我的客戶端應用程序,所以我不想在每個Web API調用都傳遞所有subCategories數據,因此我只需要請求的類別的數據和相應的計數。

讓我們假設有 '貓1'> '貓1 1'> '貓111'

'貓1' 恰好包含2個亞類。 '貓1 1'包含一個子類別。我可以通過調用Web API方法 '的getCategory(空)' 檢索 '貓1' 的數據,並檢索 '貓1' 的數據我會叫 '的getCategory(GuidOfCat11)'

'貓1':

TotalOfDirectSubCategories - 1

TotalOfAllSubCategories - 2

再次,無子將被納入

回答

0

這裏是最好的我可以實現,你覺得有一個更好的方法:

AutoMapper:

  cfg.CreateMap<Category, CategoryViewModel>() 
      .PreserveReferences() 
      .ForMember(x => x.TotalOfDirectSubCategories, x => x.MapFrom(z => z.SubCategories.Count)) 
      .ReverseMap(); 

填充CategoryViewModel:

 public List<CategoryViewModel> GetAllCategoriesAndTasksForParentCategoryAndUser(Guid? categoryId) 
    { 
     var data = new List<Category>(); 

     if (categoryId == null) // retrieve root level categories 
     { 
       data = _ctx.Categories.Where(x => x.ParentCategoryId == null) 
        .Include(x => x.SubCategories) 
        .Include(x => x.Tasks) 
        .ToList(); 
     } 
     else 
     { 
       data = _ctx.Categories.Where(x => x.CategoryId == categoryId) 
        .Include(x => x.SubCategories) 
        .Include(x => x.Tasks) 
        .ToList(); 
     } 

     var dataToReturn = Mapper.Map<List<Category>, List<CategoryViewModel>>(data); 
     foreach (var category in data) 
     { 
      var vm = dataToReturn.First(x => x.CategoryId == category.CategoryId); 

      //subCategories 
      int totalCount = 0; 
      SubCategoriesCount(ref totalCount, category); 
      vm.TotalOfAllSubCategories = totalCount;    
     } 

     return dataToReturn; 

    } 

最後,方法:

private void SubCategoriesCount(ref int subCatCount, Category category) 
    { 
     if (category.SubCategories != null || category.SubCategories.Count() != 0) 
     { 
      subCatCount += category.SubCategories.Count(); 

      foreach (var subCat in category.SubCategories) 
      { 
       SubCategoriesCount(ref subCatCount, subCat); 
      } 
     } 
    } 
相關問題