7

我有以下空间FilterDefinition

var filter = Builders<MyDocument>
                .Filter
                .Near(x => x.Point, point, 1000);

有没有办法将它包含在IQueryable表达式中?

例如,我可能有以下 LINQ 语句。我怎样才能包括上述条件?据我所知,空间查询没有 LINQ 支持。

return Database
    .GetCollection<Places>("Places")
    .AsQueryable()
    .Where(x => x.StartDate.Date <= date)
    .Where(x => x.EndDate.Date >= date)
    .Where(x => keys.Contains(selectedKeys))
    .ToList();

我正在使用新的 2.2.2 库。

4

2 回答 2

5

As of 2.4, you can use Inject() to accomplish this.

See: https://mongodb.github.io/mongo-csharp-driver/2.4/apidocs/html/M_MongoDB_Driver_Linq_LinqExtensions_Inject__1.htm

With the example code provided (corrected slightly) this would be:

var filter = Builders<Places>
                .Filter
                .Near(x => x.Point, point, 1000);

return Database
    .GetCollection<Places>("Places")
    .AsQueryable()
    .Where(x => x.StartDate.Date <= date)
    .Where(x => x.EndDate.Date >= date)
    .Where(x => keys.Contains(selectedKeys))
    .Where(x => filter.Inject())
    .ToList();
于 2020-05-28T23:46:11.460 回答
5

.NET 驱动程序 jira 项目中有一个功能请求:https ://jira.mongodb.org/browse/CSHARP-1445 。所以,目前的答案是否定的,但希望很快。

但是,FilterDefinitionBuilder (https://github.com/mongodb/mongo-csharp-driver/blob/master/src/MongoDB.Driver/FilterDefinitionBuilder.cs#L1286)上有一个“Where”方法,可以让您将 LINQ 谓词包含到正常的查找/聚合查询中。

于 2016-02-01T13:59:33.127 回答