oData筛选器不适用于MongoDB和Web API的导航属性 [英] oData filter not working on navigation property with MongoDB and Web API

查看:263
本文介绍了oData筛选器不适用于MongoDB和Web API的导航属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

控制器看起来像

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

我在MongoDb存储库中返回

集合的AsQueryable.

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

MongoDB Document looks like

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

Id是使用ObjectId.GenerateNewId().ToString()生成的,因此它们以字符串形式存储.

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

Node和NodeGroup是纯POCO

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

问题

oData URI

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'

正常工作.

但是当我尝试过滤导航属性

But when i try to filter on Navigation Property

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

它给了我例外

消息:无法确定表达式的序列化信息:ConditionalExpression.",

推荐答案

我使用了_collection.FindAll();而且有效.

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

当我不使用mongoDB进行内存收集时,它为我工作.也是.

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

mongodb的c#驱动程序中的AsQueryable方法有点问题.

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

这篇关于oData筛选器不适用于MongoDB和Web API的导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆