2013-02-17 144 views
0

我有以下LINQ查詢實體框架LINQ查詢加上SQL函數嵌套查詢

public IEnumerable<DealershipWithDealersViewModel> Get(float latitude, float longitude) 
     { 
      return from dealer in Db.Dealerships 
          join i in Db.NearestDealers(latitude, longitude) 
          on dealer.DealerID equals i.DealerID 
          select new DealershipWithDealersViewModel 
            { 
             DealerID = dealer.DealerID, 
             Dealer = dealer.Dealer, 
             DoSales = dealer.DoSales, 
             DoService = dealer.DoService, 
             AddressProvinceID = dealer.AddressProvinceID, 
             AddressLocationID = dealer.AddressLocationID, 
             Address1 = dealer.Address1, 
             Address2 = dealer.Address2, 
             Tel = dealer.Tel, 
             Fax = dealer.Fax, 
             MapLat = dealer.MapLat, 
             MapLong = dealer.MapLong, 
             Location = dealer.Location.LocationName, 
             DealerUsers = dealer.DealerUsers 
              .Select(y => new DealerUserViewModel 
                  { 
                   DealerUserID = y.DealerUserID, 
                   FirstName = y.Firstname, 
                   Surname = y.Surname, 
                   LandLine = y.LandLine, 
                   Email = y.Email, 
                   Position = y.DealerType.DealerPosition 
                  }) 

            }; 
     } 

enter image description here 我不斷收到以下錯誤嵌套查詢不具備相應的按鍵。我在網上找不到任何關於它的事情。如果我在沒有DealerUsers的情況下加載上面的代碼,它會按預期工作,但我需要嵌套數據。謝謝!下面的工作方式。

public IEnumerable<DealershipWithDealersViewModel> Get(float latitude, float longitude) 
     { 
      return from dealer in Db.Dealerships 
          join i in Db.NearestDealers(latitude, longitude) 
          on dealer.DealerID equals i.DealerID 
          select new DealershipWithDealersViewModel 
            { 
             DealerID = dealer.DealerID, 
             Dealer = dealer.Dealer, 
             DoSales = dealer.DoSales, 
             DoService = dealer.DoService, 
             AddressProvinceID = dealer.AddressProvinceID, 
             AddressLocationID = dealer.AddressLocationID, 
             Address1 = dealer.Address1, 
             Address2 = dealer.Address2, 
             Tel = dealer.Tel, 
             Fax = dealer.Fax, 
             MapLat = dealer.MapLat, 
             MapLong = dealer.MapLong, 
             Location = dealer.Location.LocationName 

            }; 
     } 

enter image description here

更新

這也適用。

return Db.Dealerships.Select(x => new DealershipWithDealersViewModel 
      { 
       DealerID = x.DealerID, 
       Dealer = x.Dealer, 
       DoSales = x.DoSales, 
       DoService = x.DoService, 
       AddressProvinceID = x.AddressProvinceID, 
       AddressLocationID = x.AddressLocationID, 
       Address1 = x.Address1, 
       Address2 = x.Address2, 
       Tel = x.Tel, 
       Fax = x.Fax, 
       MapLat = x.MapLat, 
       MapLong = x.MapLong, 
       Location = x.Location.Location1, 
       DealerUsers = x.DealerUsers.Select(y => new DealerUserViewModel 
                  { 
                   DealerUserID = y.DealerUserID, 
                   FirstName = y.Firstname, 
                   Surname = y.Surname, 
                   LandLine = y.LandLine, 
                   Email = y.Email, 
                   Position = y.DealerType.DealerType1 
                  }) 
      }); 

enter image description here

+0

是的,我是。實體框架5. – TYRONEMICHAEL 2013-02-17 18:57:31

+1

你可以嘗試'DealerID = dealer.DealerId'而不是'DealerID = i.DealerId'嗎? – Pawel 2013-02-17 19:05:40

+0

我做了並沒有區別。有點難倒了這一個。將更新答案,以免人們困惑。 – TYRONEMICHAEL 2013-02-17 19:14:50

回答

0

這是一個可複合的問題。 EF總是會嘗試將您的查詢轉換爲SQL。如果這個成功,那很好。如果沒有,它不會嘗試並使其工作,例如通過切換到linq下的引擎蓋下的對象(如linq to sql可能)。您試圖將存儲過程結果加入到SQL查詢中。這甚至不能在平面SQL中完成,因爲sproc結果不可組合,更不用說EF。

您只能使用Db.Dealerships.AsEnumerable()Db.NearestDealers(latitude, longitude)將結果加入內存中。

因此,如果您可以將過濾器參數DealerID添加到過程的簽名將會非常有用。

+0

感謝您的幫助。剛剛寫完一個存儲過程並完全繞過了linq到sql。 – TYRONEMICHAEL 2013-02-21 07:01:27

1

你在做什麼是不可能的。參見:MSDN。在文章的底部,它規定

那些需要從嵌套 查詢拉起鍵某些類型的查詢不被支持。