【问题标题】:oData filter not working on navigation property with MongoDB and Web APIoData 过滤器不适用于 MongoDB 和 Web API 的导航属性
【发布时间】:2016-04-07 02:38:14
【问题描述】:

控制器看起来像

    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。

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

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

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

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

问题

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'

工作正常。

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

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

它给了我例外

消息:“无法确定表达式的序列化信息:ConditionalExpression。”,

【问题讨论】:

  • 您的最后一个示例 URI 不包括 $expand 选项。
  • 我使用了展开相同的结果。
  • 您更新的 URI 使用 Microsoft.AspNet.OData 版本 5.7.0(最新稳定版本)生成正确的过滤结果。你用的是什么版本?
  • 我应该指出,在我的测试中,我使用了带有预设数据的假 NodeService。我没有使用 MongoDB。
  • 当我在不使用 mongoDB 的情况下用于内存收集时,它对我有用。 mongodb的c#驱动中的AsQueryable方法有问题。无论如何,请检查我的答案以进行修复。

标签: c# mongodb odata mongodb-csharp-2.0 asp.net-web-api-odata


【解决方案1】:

我使用了 _collection.FindAll();它奏效了。

当我在不使用 mongoDB 的情况下用于内存收集时,它对我有用。也一样。

mongodb的c#驱动中的AsQueryable方法有问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 2019-03-22
    • 2013-01-14
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    相关资源
    最近更新 更多