簡體   English   中英

實體框架Linq查詢加Sql函數嵌套查詢

[英]Entity Framework Linq Query Plus Sql Function Nested Query

我有以下linq查詢

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
                                                             })

                                     };
        }

在此處輸入圖片說明 我不斷收到以下錯誤嵌套查詢沒有適當的鍵 我在網上找不到任何有關它的信息。 如果我在沒有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

                                     };
        }

在此處輸入圖片說明

更新資料

這也有效。

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
                                                            })
            });

在此處輸入圖片說明

您正在做的事情可能無法實現。 請參閱: MSDN 在文章的底部指出

不支持某些需要從嵌套查詢中拉出鍵的查詢。

這是一個可混性的問題。 EF將始終嘗試將您的查詢轉換為SQL。 如果成功,那就很好。 如果沒有,它就不會嘗試使其工作,例如通過將linq切換到引擎蓋下的對象(如linq to sql一樣)。 您試圖將存儲過程結果連接到SQL查詢。 甚至在普通SQL中也無法做到這一點,因為存儲過程的結果是不可組合的,更不用說EF了。

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM