首先,我有一个带2个参数(经度,纬度)的函数。
RETURNS TABLE
AS
RETURN
(
select dbo.GeoCalculateDistance (@lat1Degrees,@lon1Degrees,Latitude,Longitude) as Distance, PKRestaurantId as PkKeyId from StRestaurant
)
正如你所知,我有一张名为StRestaurant的桌子。在这张表中,我有4列(PkRestaurantId,RegionId,Longitude,Latitude)。
而且,我需要一个带4个参数的方法。
public List<RestaurantDetailDto> GetRestaurant(int regionid, decimal latitude, decimal longitude, OrderType orderType)
{}
这种方法会给我周围的食物。但如果我想用距离系统化这个列表,我必须加入我的餐桌和功能。这是我的询问。
var query = from restaurant in context.StRestaurant
join distance in context.CalculateDistanceTable(latitude, longitude) on restaurant.PKRestaurantId equals distance.PkKeyId
where restaurant.FKRegionId == regionid
select new
{
Restaurant = restaurant,
DistanceTable = distance,
};
然后我正在检查orderType,
switch (orderType)
{
case OrderType.Distance:
query = query.OrderBy(x => x.DistanceTable.Distance);
break;
// and the anothers
}
最后,我试图将此列表作为;
var queryResult = query.ToList();
我一直犯这个错误:
嵌套查询没有相应的密钥。
我也尝试上面的查询,但它返回相同的错误:s
var query = context.StRestaurant.Where(x => x.FKRegionId == regionid && x.IsActive).Join(
context.CalculateDistanceTable(latitude, longitude),
restaurant => restaurant.PKRestaurantId,
result => result.PkKeyId,
(restaurant, result) => new
{
Restaurant = restaurant,
MinumumPackagePrice = restaurant.StRestaurantRegionRelation.FirstOrDefault(x => x.FKRestaurantId == restaurant.PKRestaurantId).MinumumPackageCharge,
DistanceTable = result,
RestaurantImage = restaurant.StRestaurantImage.Where(x => x.IsDefault && x.FKRestaurantId == restaurant.PKRestaurantId),
}
);
请帮忙!!
答案 0 :(得分:1)
我在结果上执行.Include()之前就已经看过了。我想你的投影(在第二个例子中)可能在内部做这个。你可以把它添加到第一部分吗?
在这种情况下,我必须在源表上添加.Include()
:
from a in context.A.Include("relationship")
join b in context.MyFunction(...)
...
答案 1 :(得分:0)
你可以尝试一些东西。首先,重写SQL函数,使其具有主键:
CREATE FUNCTION CalculateDistanceTable
(
-- Add the parameters for the function here
@lat1Degrees float,
@lon1Degrees float
)
RETURNS
@RestaurantDistances TABLE
(
-- Add the column definitions for the TABLE variable here
PkKeyId int NOT NULL primary key,
Distance float NOT NULL
)
AS
BEGIN
INSERT INTO @RestaurantDistances
SELECT dbo.GeoCalculateDistance(@lat1Degrees, @lon1Degrees, Latitude, Longitude) AS Distance, PKRestaurantId AS PkKeyId
FROM StRestaurant
RETURN
END
GO
此外,您可以尝试更改LINQ联接以使用匿名类型来执行联接。
var query = from restaurant in context.StRestaurant
join distance in context.CalculateDistanceTable(latitude, longitude) on new { Key = restaurant.PKRestaurantId } equals new { Key = distance.PkKeyId }
where restaurant.FKRegionId == regionid
select new
{
Restaurant = restaurant,
DistanceTable = distance,
};
如果其中任何一个都没有让我知道,我会尝试酌情更新此答案。