当我执行以下代码时:
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())
});