本文介绍了LinqToMongo注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用MongoDB的.NET应用程序,所以我正在使用Mongo C#驱动程序.

I have a .NET app which uses MongoDB so I am using the Mongo C# driver.

我当前使用的驱动程序版本为1.9.2,我的应用程序中的所有内容均按预期运行.但是,我试图将Mongo驱动程序升级到最新版本2.7.0,但是在使某些功能正常工作时遇到了一些问题.

The driver version I am currently using is 1.9.2 and everything in my app works as expected. However I am trying to upgrade the Mongo driver to latest version 2.7.0 however I am having some issues getting some things working.

下面的代码段是我遇到最新版本驱动程序问题的地方

The below Code snippet is where I am facing an issue with latest version of driver

    public IQueryable<Location> SearchByLocationDistance(double latitude, double longitude, double rangeInKm)

    {        

        var rangeInMeters = rangeInKm * 1000;        

        var point = GeoJson.Point(GeoJson.Geographic(longitude, latitude));

        var locationClause = Query<Location>.Near(y => y.Address.GeoPoint, point, rangeInMeters);

        var query = collection.AsQueryable().Where(x => locationClause.Inject());



        return query;

    }

我收到的错误消息是:

LinqToMongo.Inject方法仅适用于LINQ Where子句.

做一些研究,似乎已解决了这个问题- https://jira .mongodb.org/browse/CSHARP-1445

Doing some research it looks like a fix was checked in for this - https://jira.mongodb.org/browse/CSHARP-1445

但是我不确定是否需要使用新语法来实现相同的功能.

However I am not sure if I need to use new syntax in order to achieve the same functionality.

我尝试如下更改代码:

var filter = Builders<Location>.Filter.Near(y => y.Address.GeoPoint, point, rangeInMeters);

var query = collection.AsQueryable().Where(x => x.filter.Inject());

但是使用该代码,我得到了另一条错误消息-在$ match聚合中不允许$ earear

but with that code I get a different error message - $near is not allowed inside of a $match aggregation

推荐答案

使用下面的代码来解决此问题:

Got this working with the below code:

    public IQueryable<Location> SearchByLocationDistance(double latitude, double longitude, double rangeInKm)
    {         
        var rangeInMeters = rangeInKm * 1000;         
        var point = GeoJson.Point(GeoJson.Geographic(longitude, latitude)); //long, lat
        var filter = Builders<Location>.Filter.Near(y => y.Address.GeoPoint, point, rangeInMeters);

        return collection.Find(filter).ToList().AsQueryable();
    }

这篇关于LinqToMongo注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 19:27