Linq中的DocumentDb空间距离返回奇怪的结果

时间:2015-08-20 13:17:48

标签: c# linq azure azure-cosmosdb

当我对两个Point(Microsoft.Azure.Documents.Spatial)执行documentdb linq距离查询时,我得到的结果不同。

下面的测试返回一个包含一个LocationDocument的List。 test2返回一个空列表。 isEqual bool虽然返回true,所以我不明白为什么它们会返回不同的结果。我手动确认经度和纬度也一样。

// one document
var test = MyCollectionRepository<LocationDocument>
           .GetItems(x => x.Point.Distance(x.Point) < radius)
           .ToList();   

// no documents
var test2 = MyCollectionRepository<LocationDocumentDocument>
            .GetItems(x => x.Point.Distance(point) < radius)
            .ToList();

// true
bool isEqual = point.Equals(test[0].Point);

以下是从存储库调用的GetItems()方法:

public static IReadOnlyCollection<T> GetItems(Expression<Func<T, bool>> predicate)
{
    var items = Client.CreateDocumentQuery<T>(Collection.DocumentsLink)
        .Where(predicate)
        .ToList();
    return items;
}

有谁知道为什么会这样?它不是最简单的调试方法,因为距离调用仅在documentdb中作为查询运行时可用。

提前致谢。

点对象:

point.Position.Latitude.ToString(); //18.4239
test[0].Point.Position.Latitude.ToString(); //18.4239

point.Position.Longitude.ToString(); //-33.9253
test[0].Position.Longitude.ToString(); //-33.9253

编辑:

我已更新到最新版本的Microsoft.Azure.DocumentDB库(1.4.1),问题已更改。正如评论中所提到的,问题似乎与我的CultureInfo设置有关。我是非美国文化(确切地说是en-ZA)。

然后

test2抛出异常,而test1仍在输出预期结果。我收到了AggregateException。然后我只需要在查询中添加启用扫描到标题。以下是你如何做到这一点:

public static IReadOnlyCollection<T> GetItems(Expression<Func<T, bool>> predicate)
{
    var items = Client.CreateDocumentQuery<T>(Collection.DocumentsLink, new FeedOptions { EnableScanInQuery = true })
        .Where(predicate)
        .ToList();
    return items;
}

1 个答案:

答案 0 :(得分:1)

这是由于在非EN语言环境设置中构建LINQ to SQL查询时,DocumentDB SDK中的一个错误(现已修复):https://github.com/Azure/azure-documentdb-net/issues/49