C# 如何填写;ViewModel";对于动态过滤器,哪些需要获取不同实体的子级?
我使用的是ASP.NET Core MVC 2.*,并且使用的是McirosoftEntityFrameworkCore。 我有下表,括号中有关系:C# 如何填写;ViewModel";对于动态过滤器,哪些需要获取不同实体的子级?,c#,asp.net-core-mvc,entity-framework-core,C#,Asp.net Core Mvc,Entity Framework Core,我使用的是ASP.NET Core MVC 2.*,并且使用的是McirosoftEntityFrameworkCore。 我有下表,括号中有关系: Categories(1<==>*)CategoryProduct(*<==>1)Products(1<==>*)AttributeProduct(*<==>1)Attributes(*<==>1)AttributeCategories 我正在为我的“ProductAttribute”表
Categories(1<==>*)CategoryProduct(*<==>1)Products(1<==>*)AttributeProduct(*<==>1)Attributes(*<==>1)AttributeCategories
我正在为我的“ProductAttribute”表使用
我尝试过的查询如下:
使用LINQ:
var linqQuery = from c in _context.Categories
join cp in _context.CategroyProducts on c.Id equals cp.CategoryId
join p in _context.Products on cp.ProductId equals p.Id
join pa in _context.ProductAttributes on p.Id equals pa.ProductId
join a in _context.Attributes on pa.AttributeId equals a.Id
join ac in _context.AttributeCategories on a.AttributeCategoryId equals ac.Id
where c.Id == categoryId
此查询需要使用多个组,这对我来说很有挑战性
使用Include和foreach:
var includeQuery= _context.Categories
.Include(x => x.CategroyProducts)//step1
.ThenInclude(x => x.Product)//step2
.ThenInclude(x => x.ProductAttributes)//step3
.ThenInclude(x => x.Attribute)//step4
.ThenInclude(x => x.AttributeCategory);//step5
这个查询的挑战是从step1==>step5==>step4==>step3开始,我在foreach中无法实现这一点
填充ViewModel的最佳方法是什么?for循环必须是步骤1、步骤2、步骤3、步骤4,然后是步骤5。在步骤5中添加视图的新行。然后使用每个for循环中的索引项以任意顺序将数据放入行中。因此,如果for循环的索引是a、b、c、d、e,那么一行将是a.Name、e.quantity、d.color、b.price、c.value
var includeQuery= _context.Categories
.Include(x => x.CategroyProducts)//step1
.ThenInclude(x => x.Product)//step2
.ThenInclude(x => x.ProductAttributes)//step3
.ThenInclude(x => x.Attribute)//step4
.ThenInclude(x => x.AttributeCategory);//step5