2012-12-12 35 views
1

這一定很簡單,我非常密集,但我找不到一個例子來幫助我弄清楚。我想篩選我的tblAsset項目的列表,其assessmentId通過參數傳入。我能夠得到參數值好,但我不知道如何編寫查詢。Web API參數過濾

我的模型是使用模型創建嚮導從現有數據庫構建的。

感謝您的幫助!

public IEnumerable<tblAsset> GettblAssets() 
{ 
    NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query); 
    var assessmentId = nvc["aid"]; 

    //limit the assets by assessmentId somehow and return 
} 
+0

不是一部分,但你也可以訪問像這樣的查詢字符串參數(節省您不必使用HttpUtility和手動解析等)... IEnumerable GettblAssets([FromUri] int?assessmentId = null) –

回答

0

你可以使用.Where擴展方法由數據庫返回的IQueryable<tblAsset>比如:你的問題的

public IEnumerable<tblAsset> GettblAssets() 
{ 
    NameValueCollection nvc = HttpUtility.ParseQueryString(Request.RequestUri.Query); 
    var assessmentId = nvc["aid"]; 

    // TODO: you might need to adjust the property names of your model accordingly 
    // Also if the assessmentId property is an integer on your model type 
    // you will need to parse the value you read from the request to an integer 
    // using the int.Parse method 
    return db.tblAsset.Where(a => a.assessmentId == assessmentId); 
} 
+0

謝謝!這工作!是的,這很簡單,我正在密集。 – Majkeli