简体   繁体   中英

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

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.",

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.

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