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