2017-10-07 101 views
0

我知道很多問題都與此錯誤有關,但我無法找到將我的查詢轉換爲符合我的查詢的方法。我的錯誤:'LINQ to Entities does not recognize the method 'Boolean TryGetValue(Int32, System.Collections.Generic.List 1 [System.Nullable 1[System.Int32]] ByRef)' method, and this method cannot be translated into a store expression.' 我的思想正在融化!C#LINQ to Entities不能識別該方法TryGetValue

var groupedKeyAndValueOfProjectIdAndZoneIds = groupedProjectDelegationByProjectId.ToDictionary(keySelector: x => x.ProjectId, elementSelector: x => x.ZoneIds); 

...

var data = projects 
         .Select(p => new Project 
         { 
          Id = p.Id, 
          ProjectName = p.Name, 
          Zones = p.Zones.Where(z => 
           (zoneIds.Contains(z.Id) || (groupedKeyAndValueOfProjectIdAndZoneIds.TryGetValue(p.Id, out outValue) ? outValue.Contains(z.Id) : false))) 

... 鑑於groupedKeyAndValueOfProjectAndZones是Dictionary<int, List<int>>。 請幫幫我。

+0

你爲什麼不拿儘量讓你的問題更具可讀性?這些小東西可以幫助你更快地獲得答案。 –

回答

2

您遇到的問題是您正在嘗試將兩個數據源混合在一起。在LINQ to Entities的背後,你想要表達你在LINQ中表達的語句並將其轉化爲SQL查詢。換句話說,當你在LINQ中編寫一個選擇時,你會在SQL中獲得1:1映射。當你拋出一個數據字典的方式groupedKeyAndValueOfProjectAndZones進入混合LINQ to Entities不知道如何表示這是一個內存數據源,它沒有SQL等同於運行。

要解決這個問題,你需要或者包含在groupedKeyAndValueOfProjectAndZones將數據移動到數據庫中,並從那裏查詢,或者你需要提供你正在做後期的LINQ濾波,查詢實體

相關問題