【问题标题】:The nested query does not have the appropriate keys嵌套查询没有适当的键
【发布时间】:2014-03-14 17:28:22
【问题描述】:

首先,我有一个带有 2 个参数(经度、纬度)的函数。

  RETURNS TABLE 
  AS
  RETURN 
   (
    select dbo.GeoCalculateDistance (@lat1Degrees,@lon1Degrees,Latitude,Longitude) as    Distance, PKRestaurantId as PkKeyId from StRestaurant
   )

如您所知,我有一张名为 StRestaurant 的桌子。在此表中,我有 4 列(PkRestaurantId、RegionId、经度、纬度)。

而且,我需要一个带有 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),
                    }
                );

请帮忙!!

【问题讨论】:

  • 什么时候出现这个错误?
  • 当我尝试进行以下操作时出现此错误:var response = query.ToList();

标签: sql linq join linq-to-sql jointable


【解决方案1】:

我以前在对结果执行 .Include() 时看到过这一点。我想您的投影(在第二个示例中)可能在内部执行此操作。你能把它加到第一部分吗?

在这种情况下,我必须在源表上添加.Include()

from a in context.A.Include("relationship") join b in context.MyFunction(...) ...

【讨论】:

    【解决方案2】:

    您可以在这里尝试一些事情。首先,重写你的 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,
                            };
    

    如果这些都没有帮助,请告诉我,我会尝试酌情更新此答案。

    【讨论】:

    • 让我星期一试试。我会让你知道这些是否有帮助。感谢您的回答。
    • @doganilker 这些错误信息相同还是不同?
    猜你喜欢
    • 2020-04-24
    • 1970-01-01
    • 2017-05-28
    • 2013-07-07
    • 1970-01-01
    • 2015-01-31
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多