LinqToMongo Inject

百般思念 提交于 2019-12-24 18:51:56

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/51352209/linqtomongo-inject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!