获取Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1 [ProjectName.DTOs.ProjectDTO]而不是实际数据

时间:2020-09-27 08:22:19

标签: linq asp.net-core entity-framework-core

我正在开发.NET MVC Core项目。我试图通过在控制器中使用LINQ联接两个表来获取项目名称。 我的控制器代码:

public IActionResult Projects()
{
    var projectName = from p in _context.Tbl_Projects
                      join d in _context.Tbl_MyOfficeDocumets on p.Id equals d.Project_ID
                      select new ProjectDTO { ProjectName = p.ProjectName };

    ProjectDTO proj = new ProjectDTO { ProjectName = projectName; }

    return View(proj);
}

和剃刀视图页面:

<table>

<tr>
   <td>Your Projects</td>
   <td>Description</td>
   <td>Created On</td>
</tr>

@foreach(var item in Model.listOfProjects)
<tr>
   <td>@Model.ProjectName</td>  <-- Here I am getting that unusual response
   <td>@item.Description</td>
   <td>@item.CreatedOn</td>
</tr>

</table>

@Model.ProjectName显示:

获取Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1 [ProjectName.DTOs.ProjectDTO]

我想念什么吗?

1 个答案:

答案 0 :(得分:0)

您已创建查询并将其分配给DTO,这是错误的。

public IActionResult Projects()
{
    var query = from p in _context.Tbl_Projects
                join d in _context.Tbl_MyOfficeDocumets on p.Id equals d.Project_ID
                select new ProjectDTO { ProjectName = p.ProjectName };
  
    return View(query);
}