0

我有以下 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
                                                            })
            });

在此处输入图像描述

4

2 回答 2

1

你正在做的事情可能是不可能的。请参阅:MSDN。在文章的底部,它指出

不支持需要从嵌套查询中提取键的某些类型的查询。

于 2013-02-17T19:03:51.070 回答
0

这是可组合性的问题。EF 将始终尝试将您的查询转换为 SQL。如果这成功了,很好。如果没有,它不会尝试使其工作,例如通过切换到 linq 到引擎盖下的对象(就像 linq to sql 可能做的那样)。您试图将存储过程结果连接到 SQL 查询。这甚至不能在普通 SQL 中完成,因为 sproc 结果是不可组合的,更不用说 EF 了。

您只需使用Db.Dealerships.AsEnumerable()and将结果加入内存中Db.NearestDealers(latitude, longitude)

DealerID因此,如果您可以将过滤器参数添加到过程的签名中,那将非常有用。

于 2013-02-18T00:34:54.557 回答