[英]Mapping $filter from oData parameter to linq Where
我有(我認為是odata格式)這樣的網址:
http://localhost:2282/SSE.Web/History.cshtml?GetData=true&itemId=AKE-00129&pid=1&%24filter=indexof(ItemType%2C%27Attri%27)+ge+0&%24skip=0&%24top=50&%24inlinecount=allpages&_=1325589443808
這里有趣的是$ filter參數。 它的格式為“indexof(ItemType,'Attri')ge 0”
源是一個網格(來自infragistics的iggrid),它在ItemType列上過濾文本'Attri'
我的問題是:映射top和skip參數很簡單,但是如何進行過濾。 我是否需要解析它並構建自己的linq,還是有其他方法?
這是我到目前為止的代碼:
var skip = int.Parse(Request["$Skip"]);
var top = int.Parse(Request["$top"]);
var filter = Request(["$filter"]);
var db = Database.Open("SSEConnectionString");
var entries = db.Query("select * from eHistory order by timestamp desc")
Json.Write(new { results = entries.Where(????).Skip(skip).Take(top), totalRecCount = entries.Count() }, Response.Output);
謝謝你的幫助!
Larsi
如果您為igGrid使用MVC包裝器,則會為您完成映射。 以下是Infragistics jQuery幫助的引用:
“當ASP.NET MVC包裝器用於通過LINQ(IQueryable)綁定到服務器端數據時,URL中編碼的所有過濾信息都會自動轉換為LINQ表達式子句(Where子句),因此您不需要這樣做任何額外的東西,以過濾數據。“
您可以使用以下NuGet包來應用過濾器: https : //www.nuget.org/packages/Community.OData.Linq
代碼示例如下:
using System.Linq;
using Community.OData.Linq;
// dataSet in this example - any IQuerable<T>
Sample[] filterResult = dataSet.OData().Filter("Id eq 2 or Name eq 'name3'").ToArray();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.