Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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# Linq查询以获取对象列表,并为每个对象获取另一个嵌套列表_C#_Linq_Model View Controller - Fatal编程技术网

C# Linq查询以获取对象列表,并为每个对象获取另一个嵌套列表

C# Linq查询以获取对象列表,并为每个对象获取另一个嵌套列表,c#,linq,model-view-controller,C#,Linq,Model View Controller,在我的MVC应用程序中,我希望获得所有测试,它们是标题的父项,标题是参数的父项,参数是下拉列表的父项,例如: 测试-->标题列表-->参数列表-->下拉列表 在我的控制器中,我尝试获取列表中的列表: var getAllReport = ( from tests in _db.tblPatientSelectedTests.ToList() from heading in _db.tblHeadings.Where(x=>x.TestID ==

在我的MVC应用程序中,我希望获得所有
测试
,它们是
标题
的父项,标题是
参数
的父项,参数是
下拉列表
的父项,例如:

测试
-->
标题列表
-->
参数列表
-->
下拉列表

在我的控制器中,我尝试获取列表中的列表:

var getAllReport = (
           from tests in _db.tblPatientSelectedTests.ToList()
           from heading in _db.tblHeadings.Where(x=>x.TestID == tests.TestId).ToList()
           from par in _db.tblParameters.Where(x=>x.HeadingID == heading.HeadingID).ToList()
           from drp in _db.DropDownValues.Where(x =>x.ParemeterID == par.ParameterId).ToList()
           select new
           {
               Tests = tests,
               Headings = heading,
               Parameters = par,
               DropDowns = drp
           }
           ).ToList();
ViewBag.GetAllReports = getAllReport;
我希望它是强类型的,所以我尝试创建如下类:

public class allparams
    {
        public List<tblPatientSelectedTest> Tests { get; set; }
        public List<tblHeading> Headings { get; set; }
        public List<tblParameter> Parameters { get; set; }
        public List<DropDownValue> DropDowns { get; set; }
    }
@foreach(Tests item in ViewBag.GetAllReports)
{
 do some stuff 
@foreach(Headings item in ViewBag.GetAllReports)
 {
  do some stuff and so on for other nested lists 
 }
}

我想你用的是EF

您的问题可以通过Linq的
Include()
方法解决

var getAllReport = _db.tblPatientSelectedTests
.Include("tblHeadings")
.Include("tblParameters")
.Include("DropDownValue")
.ToList();
Include()
允许您从数据库中获取相关实体,作为同一查询的一部分。细节可见一斑

以后您可以按如下方式使用它:

@foreach(Tests test in ViewBag.GetAllReports)
{
    //do some stuff 
    @foreach(Headings heading in test.tblHeadings)
    {
        //do some stuff and so on for other nested lists 
        //and so on... for tblParameters and DropDownValue
    }
}

不过,这只是一个建议,出于可维护性的原因,数据访问逻辑应该保留在单独的层中。

我想您使用的是EF

您的问题可以通过Linq的
Include()
方法解决

var getAllReport = _db.tblPatientSelectedTests
.Include("tblHeadings")
.Include("tblParameters")
.Include("DropDownValue")
.ToList();
Include()
允许您从数据库中获取相关实体,作为同一查询的一部分。细节可见一斑

以后您可以按如下方式使用它:

@foreach(Tests test in ViewBag.GetAllReports)
{
    //do some stuff 
    @foreach(Headings heading in test.tblHeadings)
    {
        //do some stuff and so on for other nested lists 
        //and so on... for tblParameters and DropDownValue
    }
}

不过,这只是一个建议,出于可维护性的原因,数据访问逻辑应该保留在单独的层中。

考虑让类测试包含ListHeaders属性,Heading包含List Parameters属性,Parameter包含List下拉列表,即,反映我所理解的对象模型的层次结构。要想弄清楚如何将它们粘合在一起,可以使用Linq2Sql和Entity Framework来查看它们的功能(或者直接使用它们)。可以在db中作为存储过程来执行此操作,并将层次数据返回到所需的对象。您希望
@foreach(ViewBag.GetAllReports中的标题项)
做什么?是否遍历所有报告的所有标题?这似乎不是很有用。难道你不希望每个报告都有一个单独的标题列表吗?FrEoDo57谢谢。这是一个很好的选择。顺便问一下,你当前的LINQ发生了什么?考虑包含LISTADING属性的类测试,以及具有列表参数属性的标题和具有列表DROPDONE的参数,即反映我理解的层次结构是您的对象模型。要想弄清楚如何将它们粘合在一起,可以使用Linq2Sql和Entity Framework来查看它们的功能(或者直接使用它们)。可以在db中作为存储过程来执行此操作,并将层次数据返回到所需的对象。您希望
@foreach(ViewBag.GetAllReports中的标题项)
做什么?是否遍历所有报告的所有标题?这似乎不是很有用。你不希望每个报告都有一个单独的标题列表吗?Flydog57谢谢,顺便问一下,这是一个很好的选择。你现在的linq发生了什么?