oData filter not working on navigation property with MongoDB and Web API

∥☆過路亽.° 提交于 2019-12-25 06:46:17

问题


Controller looks like

    public class NodesRestController : ODataController
{
    private INodeService _nodeService;
    public NodesRestController(INodeService nodeService)
    {
        _nodeService = nodeService;
    }
    [EnableQuery()]
    public IQueryable<Node> Get()
    {
        return _nodeService.GetAllNodes();
    }
    [EnableQuery()]
    public Node Get(string id)
    {
        return _nodeService.GetNodeById(id);
    }
}

in MongoDb repository i am returning AsQueryable of the collection.

//..............Rest of initializations



 _collection = _dbContext.Database
            .GetCollection<TEntity>(typeof(TEntity).Name);
//..........

    public IQueryable<TEntity> GetAll()
    {
        return _collection.AsQueryable();
    }



public TEntity Insert(TEntity entity)
    {
        entity.Id = ObjectId.GenerateNewId().ToString();
         _collection.Insert(entity);
        return entity;
    }

    //..............Rest of initializations

MongoDB Document looks like

{
"_id" : "5688d5b1d5ae371c60ffd8ef",
"Name" : "RTR1",
"IP" : "1.2.2.22",
"NodeGroup" : {
    "_id" : "5688d5aad5ae371c60ffd8ee",
    "Name" : "Group One",
    "Username" : null,
    "Password" : null
}}

Id were generated using ObjectId.GenerateNewId().ToString() so they are stored as string.

Node and NodeGroup are pure POCOs

public partial class NodeGroup : EntityBase
{
    public string Name { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
    public string LoginPrompt { get; set; }
    public string PasswordPrompt { get; set; }
    public string ReadyPrompt { get; set; }
    public string Description { get; set; }
}
 public partial class Node : EntityBase
{
    public string Name { get; set; }
    public string IP { get; set; }
    public virtual NodeGroup NodeGroup { get; set; }
}
public abstract class EntityBase 
{
    //[JsonIgnore]
    // [BsonRepresentation(BsonType.ObjectId)]
    // [BsonId]
    public string Id { get; set; }
}

Problem

oData URIs like

http://localhost:9910/api/NodesRest
http://localhost:9910/api/NodesRest?$expand=NodeGroup
http://localhost:9910/api/NodesRest?$expand=NodeGroup&$filter=Name eq 'RTR1'

works fine.

But when i try to filter on Navigation Property

http://localhost:9910/api/NodesRest?$expand=NodeGroup&$filter=NodeGroup/Name eq 'Group One'

it gives me exception

message: "Unable to determine the serialization information for the expression: ConditionalExpression.",


回答1:


I used _collection.FindAll(); and it worked.

It worked for me when i used in memory collection without using mongoDB. As well.

It's somethings wrong with the AsQueryable method in c# driver of mongodb.



来源:https://stackoverflow.com/questions/34574958/odata-filter-not-working-on-navigation-property-with-mongodb-and-web-api

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