C# 动态视图组件菜单的LINQ语句问题
我的网站上有三级(类别-子类别-嵌套类别)下拉式导航菜单,其中数据必须动态来自数据库。我的主要问题是如何生成InvokeAsync()方法使其工作。我可以编写两个级别,这两个级别在我检查时运行良好,但在定义嵌套类别时感到困惑-需要从派生自类别的子类别中获取它。 这是我的控制器C# 动态视图组件菜单的LINQ语句问题,c#,asp.net,linq,asp.net-core,C#,Asp.net,Linq,Asp.net Core,我的网站上有三级(类别-子类别-嵌套类别)下拉式导航菜单,其中数据必须动态来自数据库。我的主要问题是如何生成InvokeAsync()方法使其工作。我可以编写两个级别,这两个级别在我检查时运行良好,但在定义嵌套类别时感到困惑-需要从派生自类别的子类别中获取它。 这是我的控制器 public class MenuViewComponent: ViewComponent { private readonly SamirDbContext _samirDbContext; pub
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);
}
}
公共类菜单ViewComponent:ViewComponent
{
私有只读SamirDbContext_SamirDbContext;
公共菜单ViewComponent(SamirDbContext SamirDbContext)
{
_samirDbContext=samirDbContext;
}
公共异步任务InvokeAsync()
{
var menu=await\u samirDbContext.Categories.Include(x=>x.Subcategories)。然后Include(y=>y.NestedCategories)。
选择(x=>newmenusmodel()
{
类别=x,
Id=x.Id,
子类别=x。子类别,
**NestedCategories=**
}).ToListAsync();
返回视图(菜单);
}
}
以下是模型:
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; }
}
公共类类别
{
公共类别()
{
子类别=新HashSet();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共ICollection子类别{get;set;}
}
公共类子类别
{
公共子类别()
{
Posts=newhashset();
NestedCategories=新HashSet();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共类别{get;set;}
public int CategoryId{get;set;}
公共ICollection Posts{get;set;}
公共ICollection嵌套类别{get;set;}
}
公共类嵌套类别
{
公共嵌套类别()
{
Posts=newhashset();
}
公共int Id{get;set;}
公共字符串名称{get;set;}
公共ICollection Posts{get;set;}
公共子类别子类别{get;set;}
公共int子类别ID{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; }
}
公共类菜单模型
{
公共int Id{get;set;}
公共类别{get;set;}
公共IEnumerable类别{get;set;}
公共IEnumerable子类别{get;set;}
公共子类别子类别{get;set;}
公共IEnumerable嵌套类别{get;set;}
公共嵌套类别嵌套类别{get;set;}
}
请帮助完成InvokeAsyinc()方法,以便获得三级菜单的工作。您可以使用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();
查看您的
类别
、子类别
和嵌套类别
模型,我想问自己,为什么在最终的选择
语句中,您实际上需要有一个单独的输出属性(**NestedCategories=…**
)
让我们这样想,如果嵌套类别
是在子类别
集合中定义的,那么每个子类别
元素都应该有自己的嵌套类别
-ies列表,当您从下拉列表中选中一些子类别
时,这些列表将可用
因此,我的建议是将结果保留如下:
var menu=await\u samirDbContext.Categories
.包括(x=>x.子类别)
.然后包括(y=>y.NestedCategories)
.Select(x=>newmenusmodel()
{
类别=x,
Id=x.Id,
子类别=x。子类别
.选择(sb=>new Subcategory to
{
某人身份证,
姓名,
...
NestedCategories=sb.NestedCategories
.选择(nst=>new NestedCategoriesDTO
{
nst.Id,
nst.名称,
...
})
}),
}).ToListAsync();
然后您可以在UI中使用上述模型。希望这会有所帮助