c# - C# Azure MongoDB Linq Near 查询
问题描述
我正在尝试将带有位置点的查询发送到 Azure Cosmos DB (MongoDB) 并获取该点附近的位置。我从这里得到了大部分信息。我正在使用Microsoft.Azure.Documents.Spatial;
.
到目前为止,我已经尝试了许多其他旧帖子的解决方案,但对我没有任何帮助。
我的班级定义:
public class Place : INotifyPropertyChanged
{
[BsonId(IdGenerator = typeof(CombGuidGenerator))]
public Guid Id { get; set; }
Point _location;
[BsonElement("Location")]
public Point Location
{
get => _location;
set
{
if (_location == value)
return;
_location = value;
HandlePropertyChanged();
}
}
long _addedDate;
[BsonElement("AddedDate")]
public long AddedDate
{
get => _addedDate;
set
{
if (_addedDate == value)
return;
_addedDate = value;
HandlePropertyChanged();
}
}
void HandlePropertyChanged([CallerMemberName]string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
蒙哥连接:
// APIKeys.Connection string is found in the portal under the "Connection String" blade
MongoClientSettings settings = MongoClientSettings.FromUrl(
new MongoUrl(APIKeys.ConnectionString)
);
settings.SslSettings =
new SslSettings() { EnabledSslProtocols = SslProtocols.Tls12 };
// Initialize the client
var mongoClient = new MongoClient(settings);
// This will create or get the database
var db = mongoClient.GetDatabase(dbName);
// This will create or get the collection
var collectionSettings = new MongoCollectionSettings { ReadPreference = ReadPreference.Nearest };
PlacesCollection = db.GetCollection<Place>(collectionName, collectionSettings);
询问:
var point = new Point(22, -5);
IMongoQueryable<Place> query = PlacesCollection.AsQueryable().Where(p => p.Location.Distance(point) < 30000);
return query.ToList();
我收到以下错误return query.ToList()
:
未处理的异常:
System.InvalidOperationException:不支持 {document}{Location}.Distance(value(Microsoft.Azure.Documents.Spatial.Point))。
我不明白什么是不支持的,也不知道如何为此创建正确的查询。任何想法?
编辑:调用堆栈
0xE0 in XamFormsMaps.Core.Services.MongoService.GetPlacesNear at C:\Users\Role\source\repos\XamFormsMaps\XamFormsMaps\XamFormsMaps.Core\Services\MongoService.cs:84,13 C#
[External Code]
0x20 in XamFormsMaps.Core.Services.DatabaseService.GetPlacesNear at C:\Users\Role\source\repos\XamFormsMaps\XamFormsMaps\XamFormsMaps.Core\Services\DatabaseService.cs:20,13 C#
0x7A in XamFormsMaps.Core.ViewModels.MapViewModel.GetPlacesAsync at C:\Users\Role\source\repos\XamFormsMaps\XamFormsMaps\XamFormsMaps.Core\ViewModels\MapViewModel.cs:112,17 C#
0x6D in XamFormsMaps.Core.Pages.MapPage.<-ctor<-ctor>b__0_1>d at C:\Users\Role\source\repos\XamFormsMaps\XamFormsMaps\XamFormsMaps.Core\Pages\MapPage.xaml.cs:27,17 C#
解决方案
您是否碰巧拥有解决方案之前的异常堆栈跟踪?我想看看是什么问题。
推荐阅读
- scrapy - 如何仅为scrapy spider中的特定请求更改标头?
- routes - Deno - opine 路由器。如何从页面中读取值?
- c# - 后端控制器中的空对象
- javascript - 如何检查数组中任意两个整数的和是否作为数组中的一项存在?
- c++ - 读取多个文本文件并将它们排序到一个结构中
- python - 正在使用“function.variable = something”一个全局变量
- keystore - PGP 密钥存储
- python - (AttributeError: 'NoneType' object has no attribute '__array_interface__') 错误是什么意思?
- html - 使用角度限制的提前输入
- r - 数据框每列上的多个线性模型