错误:无法在 LINQ to Entities 查询中构造实体或复杂类型

发布于 2024-12-01 20:31:32 字数 1733 浏览 0 评论 0原文

我收到此错误“System.NotSupportedException:无法在 LINQ to Entities 查询中构造实体或复杂类型“MyModel.Team”。”当我导航到 Team/Index/{id} 页面时。有人可以指出我犯的错误吗?

控制器:

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

I get this error "System.NotSupportedException: The entity or complex type 'MyModel.Team' cannot be constructed in a LINQ to Entities query." when I navigate to the Team/Index/{id} page. Can someone point me to the mistake I did please?

Controller:

public ActionResult Index(int id)
    {
        IQueryable<Team> teams = teamRepository.GetTeamByPersonID(id);   
        return View("Index", teams);
    }

Repository:

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

ViewModel:

public IQueryable<Team> teamList { get; set; }
public TeamViewModel(IQueryable<Team> teams)
    {
         teamList = teams;
    }

View:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愛放△進行李 2024-12-08 20:31:32

问题在于您正在 select 语句中创建一个 Team 类,而 LINQ to SQL 不支持该类。将您的 select 更改为:

select t

或使用匿名类型:

select new
{
    TeamID = t.TeamID,
    FullName = t.FullName,
    ShortName = t.ShortName,
    Iso5 = t.Iso5,
    DateEstablished = t.DateEstablished,
    City = t.City,
    CountryID = t.CountryID
};

或使用 DTO(任何非实体):

select new TeamDTO
{
    TeamID = t.TeamID,
    FullName = t.FullName,
    ShortName = t.ShortName,
    Iso5 = t.Iso5,
    DateEstablished = t.DateEstablished,
    City = t.City,
    CountryID = t.CountryID
};

The problem is that you are creating a Team class in a select statement, which is not supported by LINQ to SQL. Change your select to:

select t

or use an anonymous type:

select new
{
    TeamID = t.TeamID,
    FullName = t.FullName,
    ShortName = t.ShortName,
    Iso5 = t.Iso5,
    DateEstablished = t.DateEstablished,
    City = t.City,
    CountryID = t.CountryID
};

or use a DTO (anything that is not an entity):

select new TeamDTO
{
    TeamID = t.TeamID,
    FullName = t.FullName,
    ShortName = t.ShortName,
    Iso5 = t.Iso5,
    DateEstablished = t.DateEstablished,
    City = t.City,
    CountryID = t.CountryID
};
甜味超标? 2024-12-08 20:31:32

如果类 Team 是一个实体,则无法在 linq 语句内创建它。您应该考虑创建自己的类并返回它。或者也许只是选择t

If class Team is an Entity it can not be created inside a linq statement. You should consider creating your own class and return that instead. Or maybe just select t.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文