2012-11-21 72 views
2

當我執行下面的代碼:LINQ到實體無法識別方法「System.Linq.IQueryable`1

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到實體無法識別方法 「SYST em.Linq.IQueryable`1 [DestKosher.Model.HotelModel] GetHotelsByCityId(的Int32)」方法,和這種方法不能被翻譯成 商店表達。

該方法hotelDataAccess.GetHotelsByCityId(19)返回IQueryable。任何想法,幫助或解決方案將不勝感激。問候,

馬丁

UPDATE:

該查詢最初設置,查看是否通過將一個整數到功能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()) 
}); 
+0

不要忘記給予好評和認可它是否適合您標記的答案... –

+0

你可以看看我的更新。問候, – user1124937

回答

3

通過設計,LINQ to Entities需要整個LINQ查詢表達式轉換爲服務器查詢。只有少數不相關的子表達式(在不依賴於來自服務器的結果的查詢表達式)在客戶端上評估的查詢翻譯前。不支持任何方法調用,例如GetHotelsByCityId()在這種情況下不具有已知的翻譯。

你可以像這樣

var list = _hotelDataAccess.GetHotelsByCityId(19).ToList(); 
int myVal = (from u in list 
    select u).Count(); 

比我們在設爲myVal查詢

​​

瞭解更多Deatil:LINQ to Entities, what is not supported?

+0

這就是我想:'在_entities.ViewGeographyLocation 回報(從地理位置那裏geoLocation.CountryCode == COUNTRYCODE 排序依據geoLocation.SortOrder,geoLocation.CityName 選擇新ContinentModel { ContinentCode = geoLocation.ContinentCode, ContinentName = geoLocation.ContinentName, COUNTRYCODE = geoLocation.CountryCode, 國家或地區名稱= geoLocation.CountryName, CityId = geoLocation.CityId, CITYNAME = geoLocation.CityName, CityCode = geoLocation.CityName, TOTALCOUNT =(從u在_hotelDataAccess.GetHotelsByCityId (geoLocation.CityId).ToList() 選擇你)。計數(); });」 – user1124937

0

嘗試增加命名空間 「System.Linq的」,也如果您有「System.Core」程序集引用,請檢查您的項目引用。

+0

你可以看看上面的更新。非常感謝,馬丁 – user1124937

+0

你可以發佈你的代碼_hotelDataAccess.GetHotelsByCityId(19),所以我們可以深入瞭解。 – GiantHornet

相關問題