简体   繁体   中英

LinqToMongo Inject

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

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;

    }

The error message I get is:

The LinqToMongo.Inject method is only intended to be used in LINQ Where clauses.

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.

I tried changing my code as below:

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

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

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();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM