有一个使用MongoDB的.NET应用程序,因此我正在使用mongo c#驱动程序。我正在使用的当前版本是1.9.2。
我正在尝试将其更新为最新的C#mongodb驱动程序-2.7.0。到目前为止,为了打破更改,我不得不进行大量代码更改。我在使以下内容正常工作时遇到问题
1.9.2驱动程序代码:
public IQueryable<Tuple<Location, double>> GeoNear(StagedLocation stagedLocation, int matchRadius)
{
var point = GeoJson.Point(GeoJson.Geographic(stagedLocation.Address.Longitude.Value, stagedLocation.Address.Latitude.Value)); //long, lat
var results = collection.GeoNear(new GeoNearArgs { Spherical = true, Near = point, MaxDistance = matchRadius, Limit = 20});
return results.Hits.Select(loc => Tuple.Create(loc.Document, loc.Distance)).AsQueryable();
}
我正在尝试在2.7.0版本的驱动程序中执行相同的操作,但是下面的代码不起作用:
2.7.0驱动程序代码:
public IQueryable<Tuple<Location, double>> GeoNear(StagedLocation stagedLocation, int matchRadius)
{
var point = GeoJson.Point(GeoJson.Geographic(stagedLocation.Address.Longitude.Value, stagedLocation.Address.Latitude.Value)); //long, lat
var test = collection.FindAsync<Location>(Builders<Location>.Filter.Near(l => l, point, matchRadius, 0));
return test.Result.ToList().Select(loc => Tuple.Create(loc, 0.0)).AsQueryable();
}
但是这会引发错误:
Unable to determine the serialization information for l => l.
不确定我放置的2.7.0代码是否是执行与1.9.2版本相同的GeoNear的正确方法
包括更新位置类
[Serializable]
public class Location : EntityBase, IEquatable<Location>
{
public int VersionNumber { get; private set; }
public string Name { get; set; }
public Address Address { get; set; }
}
地址类也标记为serilizable。 EntityBase类如下:
[Serializable]
public abstract class EntityBase : IEntity
{
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
public virtual DateTimeOffset LastUpdatedOn { get; set; }
public virtual DateTimeOffset CreatedOn { get; set; }
public string LastUpdatedBy { get; set; }
public string CreatedBy { get; set; }
}