2012-03-31 49 views
1

我試圖實現從MongoDB獲取數據的Api控制器。如果C#驅動程序自版本1.4以後支持LINQ,那麼支持OData的IQueryable接口將會很好。ASP.NET WebApi:使用MongoDB官方C#驅動程序的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 指定參數超出有效值的範圍。參數名稱:無法找到root IQueryable

有誰知道是什麼原因?

+0

驅動程序問題:https://jira.mongodb.org/browse/CSHARP-419?page=com.atlassian.jira.plugin.system.issuetabpanels%3Achangehistory-tabpanel – 2012-03-31 11:24:12

+0

解決方法是增加所以額外的表達式:return this.collection.AsQueryable ().Select(u => u); – asa 2012-04-08 12:40:46

回答

相關問題