C# LINQ to实体无法识别方法'System.LINQ.IQueryable`1
当我执行以下代码时:C# LINQ to实体无法识别方法'System.LINQ.IQueryable`1,c#,sql,linq,C#,Sql,Linq,当我执行以下代码时: IHotelDataAccess _hotelDataAccess= new HotelDataAccess(_entities); int myVal = (from u in _hotelDataAccess.GetHotelsByCityId(19) select u).Count(); myVal按预期返回一个整数值,但是如果我尝试按如下方式返回IQueryable return (from geoLocation in _entities.ViewGeo
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实体无法识别该方法
'System.Linq.IQueryable'1[DestKosher.Model.HotelModel]
GetHotelsByCityIdInt32'方法,无法转换此方法
转换为存储表达式
方法hotelDataAccess.GetHotelsByCityId19返回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())
});
根据设计,需要将整个LINQ查询表达式转换为服务器查询。在翻译查询之前,客户机上只计算查询中不依赖于服务器结果的少数不相关子表达式。不支持没有已知转换的任意方法调用,如本例中的GetHotelsByCityId
你可以这样做
var list = _hotelDataAccess.GetHotelsByCityId(19).ToList();
int myVal = (from u in list
select u).Count();
而不是在你的查询中使用myval
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 = myVal
});
阅读更多细节:尝试添加名称空间System.Linq,如果您有System.Core程序集引用,还可以检查您的项目引用。不要忘记升级投票并将答案标记为已接受,如果它适合您……您可以看一下我的更新吗。您好,您能看一下上面的更新吗。非常感谢,Martin你能发布你的_hotelDataAccess.GetHotelsbycity的代码吗?这样我们就可以进行更深入的分析了。这就是我想要的:'return from geoLocation in _entities.ViewGeographyLocation where geoLocation.CountryCode==CountryCode orderby geoLocation.SortOrder,geoLocation.CityName选择新模型{大陆代码=地理位置。大陆代码,大陆名称=地理位置。大陆名称,国家代码=地理位置。国家代码,国家名称=地理位置。国家名称,城市ID=地理位置。城市ID,城市名称=地理位置。城市名称,城市代码=地理位置。城市名称,总计数=来自_hotelDataAccess.gethotelsbycitygeolocation.Cit中的uyId.ToList选择u.Count;};'