简体   繁体   English

oData筛选器不适用于MongoDB和Web API的导航属性

[英]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. 在MongoDb存储库中,我返回集合的AsQueryable。

//..............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 MongoDB文档看起来像

{
"_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. 使用ObjectId.GenerateNewId()。ToString()生成ID,因此它们以字符串形式存储。

Node and NodeGroup are pure POCOs Node和NodeGroup是纯POCO

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 oData URI,例如

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.", 消息:“无法确定表达式的序列化信息:ConditionalExpression。”,

I used _collection.FindAll(); 我用了_collection.FindAll(); and it worked. 而且有效。

It worked for me when i used in memory collection without using mongoDB. 当我不使用mongoDB进行内存收集时,它为我工作。 As well. 也一样

It's somethings wrong with the AsQueryable method in c# driver of mongodb. mongodb的c#驱动程序中的AsQueryable方法有点问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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