Sql .Select with EF Core返回视图中的ModelError

Sql .Select with EF Core返回视图中的ModelError,sql,linq,asp.net-core,entity-framework-core,Sql,Linq,Asp.net Core,Entity Framework Core,我想提供一份预约名单,以及他们各自的参与者和/或受试者 Planner模型如下所示: public class Planner : BaseEntity { public int? ProjectCandId { get; set; } public int? CandidateId { get; set; } public int? ProjectId { get; set; } public int? CustomerId {

我想提供一份预约名单,以及他们各自的参与者和/或受试者

Planner模型如下所示:

public class Planner : BaseEntity
{        
    public int? ProjectCandId { get; set; }
    public int? CandidateId { get; set; }        
    public int? ProjectId { get; set; }
    public int? CustomerId { get; set; }        

    public DateTime PlanDatum { get; set; }
    public string Location { get; set; }        
    public string Description { get; set; }

    public virtual Customer Customer { get; set; }
    public virtual Candidate Candidate { get; set; }
    public virtual Project Project { get; set; }
    public virtual ProjectCand ProjectCand { get; set; }
}
我只需要从Customer、Project和Candidate中选择一些字段,因此我创建了一个控制器,看起来像:

 public async Task<IActionResult> Index()
    {
       var afspraken = await _context.Planner
            .Include(c => c.Customer)
            .Include(c => c.Project)
            .Include(c => c.Candidate)
            .Select(s => new { s.Id, s.CandidateId, s.CustomerId, s.Description,  s.IsDeleted, s.Location, s.PlanDatum, 
                s.Customer.CompanyName, 
                s.Project.ProjectTitle, 
                s.Candidate.FullName })
            .ToListAsync().ConfigureAwait(false);
        return View(afspraken);
    }

我无法在PlannerController中找到将ViewModel链接到我的数据集的正确代码
afspraken
。有什么想法吗?

您的afspraken是一个匿名列表,您正在将其传递给视图,该视图要求模型类型为
IEnumerable

将最终Select语句更改为类似以下内容:

.Select(s => new MyProject.Models.Planner {
...
})

根据您的要求,显示
Planner
及其各自的参与者和/或主题。无需指定
。选择

下面是一个工作演示,如下所示:

1.型号:

public class BaseEntity
{
    public int Id { get; set; }
    public bool IsDeleted { get; set; }
}
public class Planner : BaseEntity
{
    public int? ProjectCandId { get; set; }
    public int? CandidateId { get; set; }
    public int? ProjectId { get; set; }
    public int? CustomerId { get; set; }

    public DateTime PlanDatum { get; set; }
    public string Location { get; set; }
    public string Description { get; set; }

    public virtual Customer Customer { get; set; }
    public virtual Candidate Candidate { get; set; }
    public virtual Project Project { get; set; }
}  
public class Customer: BaseEntity
{
    public string CompanyName { get; set; }
}
public class Candidate: BaseEntity
{
    public string FullName { get; set; }
}
public class Project: BaseEntity
{
    public string ProjectTitle { get; set; }
}
2.意见:

@model IEnumerable<Planner>
<h1>Index</h1>
<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Id)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.CandidateId)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.CustomerId)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.PlanDatum)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Location)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Description)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Customer)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Candidate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Project)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.IsDeleted)
            </th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CandidateId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CustomerId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.PlanDatum)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Location)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Description)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Customer.CompanyName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Candidate.FullName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Project.ProjectTitle)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.IsDeleted)
            </td>
        </tr>
}
    </tbody>
</table>
@model IEnumerable
指数


我按照你的建议做了,但Planner不是一个IEnumerable,所以它不起作用。然后,我创建了一个List PlanList,并在.Select语句中使用它,但这也不起作用。信息是,I PlanList是“一个变量,但用作类型”,视图模型是一种方法。这是怎么“让事情变得更复杂”的呢?我正在努力让ViewModel方法发挥作用。我将在我的问题中添加一个编辑部分进行解释。谢谢您的帮助。然而,我有点困惑。我已经为Customer、Candidate和Project创建了类,并通过
公共虚拟客户客户{get;set;}等将它们包括在内。
我无法再次重新定义它们。整个目的是从该类中提取单个元素,而不是使查询膨胀。您对
重新定义它们是什么意思?
?您只需要像在后端一样搜索对象并包含子元素。然后在视图中选择所需的单个元素。您好,你现在还有问题吗?很抱歉没有回到你身边。我放弃了。如果我不使用。选择并提取完整的实体,查询工作正常。我想问题的核心(除了我的编程技能)是如何扩展列表,然后在视图中使用它。谢谢你帮我。
@model IEnumerable<Planner>
<h1>Index</h1>
<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Id)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.CandidateId)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.CustomerId)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.PlanDatum)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Location)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Description)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Customer)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Candidate)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Project)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.IsDeleted)
            </th>
        </tr>
    </thead>
    <tbody>
@foreach (var item in Model) {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CandidateId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.CustomerId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.PlanDatum)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Location)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Description)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Customer.CompanyName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Candidate.FullName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Project.ProjectTitle)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.IsDeleted)
            </td>
        </tr>
}
    </tbody>
</table>
public async Task<IActionResult> Index()
{
    var model = _context.Planner.Include(p => p.Candidate).Include(p => p.Customer).Include(p => p.Project);
    return View(await model.ToListAsync());
}