【发布时间】:2012-11-21 06:37:28
【问题描述】:
当我执行以下代码时:
IHotelDataAccess _hotelDataAccess= new HotelDataAccess(_entities);
int myVal = (from u in _hotelDataAccess.GetHotelsByCityId(19)
select u).Count();
myVal 按预期返回一个整数值,但是如果我尝试如下返回 IQueryable
return (from geoLocation in _entities.ViewGeographyLocation
where geoLocation.CountryCode == countryCode
orderby geoLocation.SortOrder, geoLocation.CityName
select new ContinentModel
{
ContinentCode = geoLocation.ContinentCode,
ContinentName = geoLocation.ContinentName,
CountryCode = geoLocation.CountryCode,
CountryName = geoLocation.CountryName,
CityId = geoLocation.CityId,
CityName = geoLocation.CityName,
CityCode = geoLocation.CityName,
TotalCount = ((from u in _hotelDataAccess.GetHotelsByCityId(19)
select u).Count())
});
我得到了错误:
LINQ to Entities 无法识别该方法 'System.Linq.IQueryable`1[DestKosher.Model.HotelModel] GetHotelsByCityId(Int32)' 方法,这个方法不能翻译 到商店表达式中。
hotelDataAccess.GetHotelsByCityId(19) 方法返回 IQueryable。任何想法、帮助或解决方案将不胜感激。问候,
马丁
更新:
最初设置此查询是为了查看将整数放入函数 GetHotelsByCityId 是否有效。但是,我最终想做的是:
return (from geoLocation in _entities.ViewGeographyLocation
where geoLocation.CountryCode == countryCode
orderby geoLocation.SortOrder, geoLocation.CityName
select new ContinentModel
{
ContinentCode = geoLocation.ContinentCode,
ContinentName = geoLocation.ContinentName,
CountryCode = geoLocation.CountryCode,
CountryName = geoLocation.CountryName,
CityId = geoLocation.CityId,
CityName = geoLocation.CityName,
CityCode = geoLocation.CityName,
TotalCount = ((from u in _hotelDataAccess.GetHotelsByCityId(geoLocation.CityId)
select u).Count())
});
【问题讨论】:
-
如果对您有用,请不要忘记投票并将答案标记为已接受...
-
你能看看我的更新吗?问候,