我收到此錯誤「System.NotSupportedException:實體或複雜類型'MyModel.Team'無法在LINQ to Entities查詢中構建。當我導航到團隊/索引/ {id}頁面時。有人能指出我犯的錯誤嗎?錯誤:無法在LINQ to Entities查詢中構造實體或複雜類型
控制器:
public ActionResult Index(int id)
{
IQueryable<Team> teams = teamRepository.GetTeamByPersonID(id);
return View("Index", teams);
}
庫:
public IQueryable<Team> GetTeamByPersonID(int id)
{
return from t in entities.Teams
join d in entities.Departments
on t.TeamID equals d.TeamID
where (from p in entities.Person_Departments
join dep in entities.Departments
on p.DepartmentID equals dep.DepartmentID
where p.PersonID == id
select dep.TeamID).Contains(d.TeamID)
select new Team
{
TeamID = t.TeamID,
FullName = t.FullName,
ShortName = t.ShortName,
Iso5 = t.Iso5,
DateEstablished = t.DateEstablished,
City = t.City,
CountryID = t.CountryID
};
}
視圖模型:
public IQueryable<Team> teamList { get; set; }
public TeamViewModel(IQueryable<Team> teams)
{
teamList = teams;
}
查看:
<% foreach (var team in Model){ %>
<tr>
<td><%: Html.ActionLink(team.ShortName, "Details", new { id=team.TeamID}) %></td>
<td><%: team.City %></td>
<td><%: team.Country %></td>
</tr>
<% } %>
謝謝你的詳細解答! – Tsarl