Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.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# 如何在ASP.NET MVC中处理分层查询_C#_Asp.net_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 如何在ASP.NET MVC中处理分层查询

C# 如何在ASP.NET MVC中处理分层查询,c#,asp.net,asp.net-mvc,entity-framework,C#,Asp.net,Asp.net Mvc,Entity Framework,如何在ASP.NET MVC中处理分层查询及其数据显示 假设我有一个模型,看起来像这样: (具有附加属性) 公共类父类{ public int ParentId{get;set;} 公共虚拟ICollection ChildsLvl1{get;set;} } 公营儿童公寓{ public int ChildLvlOneId{get;set;} 公共虚拟父级{get;set;} 公共虚拟ICollection ChildsLvl2{get;set;} } 公共类儿童2{ public int Ch

如何在ASP.NET MVC中处理分层查询及其数据显示

假设我有一个模型,看起来像这样: (具有附加属性)

公共类父类{
public int ParentId{get;set;}
公共虚拟ICollection ChildsLvl1{get;set;}
}
公营儿童公寓{
public int ChildLvlOneId{get;set;}
公共虚拟父级{get;set;}
公共虚拟ICollection ChildsLvl2{get;set;}
}
公共类儿童2{
public int ChildLvlTwoId{get;set;}
公共ChildLvlOne ChildLvlOne{get;set;}
公共虚拟ICollection SomeOtherObjects{get;set;}
}
现在我想在两个级别上显示来自父级的数据和来自子级的数据,但是来自ChildLvlTwo的数据必须以不同的方式显示,这取决于其相关的SomeOtherObjects

所以,我的第一个想法是,为ChildLvlTwo创建一个新的ViewModel,并在其中添加一个属性,该属性决定了它的显示方式。 但要做到这一点,我还必须为ChildLvlOne和Parent创建一个新的ViewModel,并在查询中创建一个非常大的select语句


对于这样的问题,有什么好的解决方案吗?

我想这关系到您希望如何显示子对象中的数据。你喜欢这项工作吗

from p in Parent
select new
{
    Parent = p,
    Child =  
    {
        ChildLvl1.ChildsLvl2.Select(l2 => ChildLv2.ChildLvlTwoId).ToList().Contains(1)  ? "Format1" :
        ChildLvl1.ChildsLvl2.Select(l2 => ChildLv2.ChildLvlTwoId).ToList().Contains(2) ? "Format2" :
        ChildLvl1.ChildsLvl2.Select(l2 => ChildLv2.ChildLvlTwoId).ToList().Contains(3) ? "Format3" : "Format4"
    }
};
from p in Parent
select new
{
    Parent = p,
    Child =  
    {
        ChildLvl1.ChildsLvl2.Select(l2 => ChildLv2.ChildLvlTwoId).ToList().Contains(1)  ? "Format1" :
        ChildLvl1.ChildsLvl2.Select(l2 => ChildLv2.ChildLvlTwoId).ToList().Contains(2) ? "Format2" :
        ChildLvl1.ChildsLvl2.Select(l2 => ChildLv2.ChildLvlTwoId).ToList().Contains(3) ? "Format3" : "Format4"
    }
};