我有以下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
})
});
答案 0 :(得分:1)
你正在做什么可能是不可能的。见:MSDN。在文章的底部说明了
某些类型的查询需要从嵌套中提取密钥 不支持查询。
答案 1 :(得分:0)
这是一个可堆肥的问题。 EF将始终尝试将您的查询转换为SQL。如果成功,那很好。如果没有,它就不会尝试让它工作,例如通过切换到linq到引擎盖下面的对象(如linq到sql可能做的那样)。您尝试将存储过程结果连接到SQL查询。这甚至不能在平原SQL中完成,因为sproc结果不可组合,更不用说EF了。
您只能使用Db.Dealerships.AsEnumerable()
和Db.NearestDealers(latitude, longitude)
将结果加入记忆中。
因此,如果您可以在过程的签名中添加过滤器参数DealerID
,那将非常有用。