我正在使用AutoMapper ProjectTo
将复杂对象映射到更简单的DTO对象:
这有效:
Dim items = From team In db.Teams.ProjectTo(Of TeamDto)
这有效:
Dim items = From team In db.Players.ProjectTo(Of TeamDto)
但这不起作用:
Dim items = From team In db.Teams.ProjectTo(Of TeamDto)
From player In dbPlayers.ProjectTo(Of PlayerDto).
Where(Function(player) player.TeamId = team.Id).
OrderByDescending(Function(player) player.TotalPoints).
Take(5)
它会导致可怕的错误:
LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1[PlayerDto] ProjectTo[Player](System.Linq.IQueryable, System.Linq.Expressions.Expression`1[System.Func`2[PlayerDto,System.Object]][])' method, and this method cannot be translated into a store expression.
关于相关查询的某些内容似乎打破了AutoMapper,但我没有尝试解决过这个问题。
UPDATE :这实际上似乎与AutoMapper无关,因为我在IQueryable上尝试使用传统的扩展方法,这在应用于Teams表时也有效,但会导致同样的问题当应用于玩家表时。所以这似乎是EF的怪异。