【问题标题】:ASP.NET WebApi: IQueryable support with MongoDB official C# driverASP.NET WebApi:使用 MongoDB 官方 C# 驱动程序支持 IQueryable
【发布时间】:2012-03-31 05:06:22
【问题描述】:

我正在尝试实现从 MongoDB 获取数据的 Api 控制器。当 C# 驱动程序从 1.4 版支持 LINQ 时,支持 OData 的 IQueryable 接口会很好。

public class UserController : ApiController
{
  private MongoCollection<User> collection;

  public UserController()
  {
    var connectionString = ConfigurationManager.ConnectionStrings["mongo"].ConnectionString;
    var database = MongoDatabase.Create(connectionString);
    this.collection = database.GetCollection<User>("users");
  }

  public IQueryable<User> Get()
  {
    return this.collection.AsQueryable<User>();
  }
}

当我尝试获取用户时,我收到以下异常: System.ArgumentOutOfRangeException 指定的参数超出了有效值的范围。参数名称:无法找到根 IQueryable

有人知道是什么原因吗?

【问题讨论】:

  • 解决方法是添加额外的表达式:return this.collection.AsQueryable().Select(u => u);

标签: mongodb iqueryable asp.net-web-api


【解决方案1】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 2020-03-27
    • 2011-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-21
    相关资源
    最近更新 更多