【问题标题】:LINQ statement issue for dynamic view component menu动态视图组件菜单的 LINQ 语句问题
【发布时间】:2020-12-18 09:16:54
【问题描述】:

我的网站上有三层(类别-子类别-嵌套类别)下拉导航菜单,其数据必须动态来自数据库。我在生成 InvokeAsync() 方法以使其工作的主要问题。我可以编写两个级别,它们在我检查时工作正常,但在定义嵌套类别时感到困惑——需要从派生自类别的子类别中获取它。 这是我的控制器

  public class MenuViewComponent: ViewComponent
{
    private readonly SamirDbContext _samirDbContext;

    public MenuViewComponent(SamirDbContext samirDbContext)
    {
        _samirDbContext = samirDbContext;
    }

    public async Task<IViewComponentResult> InvokeAsync()
    {
        var menu = await _samirDbContext.Categories.Include(x => x.Subcategories).ThenInclude(y => y.NestedCategories).
                                                  Select(x => new MenusModel()
                                                  {
                                                      Category = x,
                                                      Id = x.Id,
                                                      Subcategories = x.Subcategories,
                                                      **NestedCategories = ...**
                                                  }).ToListAsync();
         
            return View(menu);
                                                                                   
    }
}

这里是模型:

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

    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Subcategory> Subcategories { get; set; }
}

public class Subcategory
{
    public Subcategory()
    {
        Posts = new HashSet<Post>();
        NestedCategories = new HashSet<NestedCategory>();
    }
    public int Id { get; set; }
    public string Name { get; set; }
    public Category Category { get; set; }
    public int CategoryId { get; set; }
    public ICollection<Post> Posts { get; set; }
    public ICollection<NestedCategory> NestedCategories { get; set; }
}

public class NestedCategory
{
    public NestedCategory()
    {
        Posts = new HashSet<Post>();
        
    }
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Post> Posts { get; set; }
    public Subcategory Subcategory { get; set; }
    public int SubcategoryId { get; set; }
}

菜单视图模型

 public class MenusModel
{
    public int Id { get; set; }
    public Category Category { get; set; }
    public IEnumerable<Category> Categories { get; set; }
    public IEnumerable<Subcategory> Subcategories { get; set; }
    public Subcategory Subcategory { get; set; }
    public IEnumerable<NestedCategory> NestedCategories { get; set; }
    public NestedCategory NestedCategory { get; set; }
}

请帮助完成 InvokeAsyinc() 方法,以便获得 3 级菜单的工作。

【问题讨论】:

    标签: c# asp.net linq asp.net-core


    【解决方案1】:

    您可以使用 SelectMany() 方法,更改 linq 如下:

    var menu = await _samirDbContext.Categories
        .Select(x => new MenusModel()
        {
            Category = x,
            Id = x.Id,
            Subcategories = x.Subcategories,
            NestedCategories = x.Subcategories.SelectMany(s => s.NestedCategories).ToList()
        }).ToListAsync();
    

    【讨论】:

      【解决方案2】:

      查看CategorySubcategoryNestedCategory 的模型,我在问自己,为什么在最终的Select 语句中实际上可能需要有一个单独的输出属性(**NestedCategories = ...**)。

      如果NestedCategory 定义在Subcategory 集合中,让我们这样想,那么每个Subcategory 元素都应该有自己的NestedCategory-ies 列表,当您检查一些@987654330 时将可用@ 从下拉列表中。

      所以,我的建议是让结果如下:

      var menu = await _samirDbContext.Categories
          .Include(x => x.Subcategories)
          .ThenInclude(y => y.NestedCategories)
          .Select(x => new MenusModel()
          {
              Category = x,
              Id = x.Id,
              Subcategories = x.Subcategories
                  .Select(sb => new SubcategoryDTO
                  {
                      sb.Id,
                      sb.Name,
                      ...
                      NestedCategories = sb.NestedCategories
                          .Select(nst => new NestedCategoriesDTO 
                          {
                               nst.Id,
                               nst.Name,
                               ...
                          })
                  }), 
          }).ToListAsync();
      

      然后您可以在您的 UI 中使用上述模型。
      希望这会有所帮助))

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-09
        • 1970-01-01
        • 1970-01-01
        • 2019-08-05
        • 1970-01-01
        • 1970-01-01
        • 2020-01-19
        • 2018-10-11
        相关资源
        最近更新 更多