Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何填写;ViewModel";对于动态过滤器,哪些需要获取不同实体的子级?_C#_Asp.net Core Mvc_Entity Framework Core - Fatal编程技术网

C# 如何填写;ViewModel";对于动态过滤器,哪些需要获取不同实体的子级?

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”表

我使用的是ASP.NET Core MVC 2.*,并且使用的是McirosoftEntityFrameworkCore。 我有下表,括号中有关系:

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