尝试选择匿名类型时实体框架中出现异常
我无法理解尝试针对实体链接执行此查询时生成的异常的性质。
var internalUsersFromRepos = userRepos.Users.Where(u => u.IsInternalUser)
.OrderBy(u => u.SirName)
.Skip((int)((page - 1) * usersPerPage))
.Take((int)usersPerPage)
.Select(u => new { User = u, Count = userRepos.Users.Count() })
.ToList();
无法转换类型 'System.Data.Objects.ObjectQuery
1' 到 输入“System.Linq.IQueryable
1”。 LINQ to Entities 仅支持强制转换 实体数据模型原始类型。
如果我将“Count = userRepos.Users.Count()”替换为“Count = 3”之类的常量,那么也不会有例外,所以我相信查询的这个方面是关键。
I'm having trouble understanding the nature of the exception generated when trying to execute this query against link to entities.
var internalUsersFromRepos = userRepos.Users.Where(u => u.IsInternalUser)
.OrderBy(u => u.SirName)
.Skip((int)((page - 1) * usersPerPage))
.Take((int)usersPerPage)
.Select(u => new { User = u, Count = userRepos.Users.Count() })
.ToList();
Unable to cast the type
'System.Data.Objects.ObjectQuery1' to
1'. LINQ
type 'System.Linq.IQueryable
to Entities only supports casting
Entity Data Model primitive types.
If I replace "Count = userRepos.Users.Count()" with something constant like "Count = 3" then there is no exception so I believe this aspect of the query is key.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你不能像这样组合 ObjectQueries。而且你的查询是完全错误的。使用这个:
我知道您希望在单次往返中返回计数和分页数据,但除非您使用存储过程,否则它看起来不可能。数据和计数产生两个不同的结果集,因此它们不能轻松地作为单个结果集返回。您当前的查询尝试返回用户列表,并为每个返回的用户计算所有用户的计数(没有
IsInternalUser
过滤器)I think you can't combine ObjectQueries like this. Also your query is completly wrong. Use this:
I understand tha you want to return count and paged data in single round-trip but it doesn't look possible unless you use stored procedure. Data and count produces two different result sets so they can't be easily returned as single result set. Your current query tried to return list of users and for each returned user computed count of all users (without
IsInternalUser
filter)