View中的自定义模型出错

时间:2015-04-27 12:44:04

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 model-view-controller

我已将自定义模型用于我的视图中,当我尝试使用它时出现错误

  

无法在LINQ to Entities查询中构造实体或复杂类型“Prep2Model.TechProfile”。

我不知道如何解决它 这是我的相关课程和控制器

自定义视图模型

namespace Prep2.Models
{
public class MyProjectViewModel
{
    public IEnumerable<Project> Project { get; set; }
    public IEnumerable<MyProjectCustomObject> Data { get; set; }
}
}

MyProjectCustomObject

namespace Prep2.Models
{
public class MyProjectCustomObject
{
    public int ProjectId { get; set; }
    public string ProjectCustomer { get; set; }
    public string ProjectName { get; set; }

    public IEnumerable<TechProfile> ProjectTechProfile { get; set; }

    public int MemberId { get; set; }
    public string MemberRole { get; set; }
    public short? MemberStart { get; set; }
    public short? MemberEnd { get; set; }
}
}

控制器

        var Projects = from a in db.Project
                      select a;

        var Data = from a in db.Member
                   where a.Person.PersonId.Equals(Id)
                   select new MyProjectCustomObject
                   {
                       ProjectId = a.Project.ProjectId,
                       ProjectCustomer = a.Project.Customer,
                       ProjectName = a.Project.Name,

                       ProjectTechProfile = a.Project.TechProfile.Select(x => new TechProfile()
                       {
                           TechProfileId = x.TechProfileId,
                           Name = x.Name,
                           Elements = x.Elements
                       }),

                       MemberId = a.MemberId,
                       MemberRole = a.Role,
                       MemberStart = a.Start,
                       MemberEnd = a.End
                   };

        var MyViewModel = new MyProjectViewModel();
        MyViewModel.Project = Projects;
        MyViewModel.Data = Data;

        //return ViewModel to View.
        return View(MyViewModel);

1 个答案:

答案 0 :(得分:1)

它说的是它无法在TechProfile内创建新的select记录:

x => new TechProfile()
{
    TechProfileId = x.TechProfileId,
    Name = x.Name,
    Elements = x.Elements
}

我相信你正在寻找这个:

ProjectTechProfile = a.Project.TechProfile.First(x =>
{
    TechProfileId = x.TechProfileId,
    Name = x.Name,
    Elements = x.Elements
})

<强>更新 你有两个我可以看到的选项。首先,您可以创建一个模仿TechProfile类的其他类,并执行以下操作:

ProjectTechProfile = a.Project.TechProfile.Select(x => new MyTechProfile
{
    TechProfileId = x.TechProfileId,
    Name = x.Name,
    Elements = x.Elements
})

或者您可以先将结果发送到本地集合,从而发出SQL语句,然后将它们投影到MyProjectCustomObject

var Data = from a in db.Member
           where a.Person.PersonId.Equals(Id)
           select a;
var list = Data.ToList();
var projectedList = list.Select(a => new MyProjectCustomObject
{
    ProjectId = a.Project.ProjectId,
    ProjectCustomer = a.Project.Customer,
    ProjectName = a.Project.Name,
    ProjectTechProfile = a.Project.TechProfile.Select(x => new TechProfile()
    {
        TechProfileId = x.TechProfileId,
        Name = x.Name,
        Elements = x.Elements
    }),
    MemberId = a.MemberId,
    MemberRole = a.Role,
    MemberStart = a.Start,
    MemberEnd = a.End
});