【问题标题】:LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1LINQ to Entities 无法识别方法“System.Linq.IQueryable”1
【发布时间】: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())
 });

【问题讨论】:

  • 如果对您有用,请不要忘记投票并将答案标记为已接受...
  • 你能看看我的更新吗?问候,

标签: c# sql linq


【解决方案1】:

根据设计,LINQ to Entities 需要将整个 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 
 });

阅读更多详情:LINQ to Entities, what is not supported?

【讨论】:

  • 这就是我想要的:'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 = (来自 _hotelDataAccess.GetHotelsByCityId(geoLocation.CityId).ToList 中的 u () 选择 u).Count(); });'
【解决方案2】:

尝试添加命名空间“System.Linq”,如果您有“System.Core”程序集引用,请检查您的项目引用。

【讨论】:

  • 请您看看上面的更新。非常感谢,马丁
  • 您能否发布您的 _hotelDataAccess.GetHotelsByCityId(19) 代码,以便我们深入分析。
猜你喜欢
  • 1970-01-01
  • 2015-06-21
  • 1970-01-01
  • 2014-08-09
  • 1970-01-01
  • 2012-04-03
  • 2021-11-06
  • 2012-05-03
相关资源
最近更新 更多