2

我曾在我看來,使用個性化的模型,當我嘗試使用它,我得到的錯誤錯誤在查看自定義模型

實體或複雜類型「Prep2Model.TechProfile」不能在LINQ to Entities查詢中構造。

而且我不知道如何解決它。
這裏是我的相關類和控制器

自定義視圖模式

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); 
+0

這是實體框架而不是MVC的錯誤。 [**的StackOverflow:**實體不能在LINQ被構造成實體查詢] [1] [1]:http://stackoverflow.com/questions/5325797/the- entity-can-be-constructed-in-a-linq-to-entities-query – Dan

回答

1

什麼它說是它不能創建select內一個新的紀錄TechProfile

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 
}) 

UPDATE 你有然後我可以看到兩個選項。首先,你可以創建一個不同的類,它模仿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 
}); 
+0

但是可以有很多TechProfiles,這就是爲什麼我需要它作爲IEnumerable –

+0

@ChristianCaspovich請參閱更新。 –