Web API参数过滤

时间:2012-12-12 16:53:16

标签: asp.net-web-api

这一定很简单,而且我非常密集,但我找不到一个例子来帮助我解决这个问题。我想通过tblAsset过滤我通过参数传递的assessmentId项列表。我能够获得参数值ok,但我不确定如何编写查询。

我的模型是使用模型创建向导从现有数据库构建的。

感谢您的帮助!

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

  //limit the assets by assessmentId somehow and return
}

1 个答案:

答案 0 :(得分: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);
}