3

I've got (what I think is a odata format) url like this:

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

what is interesting here is the $filter parameter. It has the format "indexof(ItemType,'Attri') ge 0"

The source is an grid (iggrid from infragistics) that is filtering on the ItemType column with text 'Attri'

My question is: Mapping the top and skip parameters was trivial, but how to do the filter. Do I need to parse it and build my own linq, or are there some other ways?

This is the code I have so far:

        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);

Thanks for any help!

Larsi

Borislav T
  • 619
  • 4
  • 20
Larsi
  • 4,654
  • 7
  • 46
  • 75
  • Are you using WCF Data Services? That should take care of all of that for you. If not, give it a try. – Vitek Karas MSFT Jan 03 '12 at 12:45
  • No, I was just using the Database mini orm comming from WebMatrix. And I was hoping to avoid the WCF Data service. But thanks for the suggestion. It is probably the best option. – Larsi Jan 04 '12 at 13:43
  • The thing is that later when you want to support $expand and $select as well it gets really complicated. And I also doubt that the Json.Write produces OData compliant payloads. Maybe it's not a goal for you, but if it is, it becomes a problem soon as well. – Vitek Karas MSFT Jan 04 '12 at 17:30
  • @VitekKarasMSFT good point! Agree I'll run into problems later. I'll go with the wcf data service. – Larsi Jan 05 '12 at 22:07
  • I have a similar requirement. Surely there's a way to reuse WCF Data Service's mechanism that converts odata filters to LINQ queries. Clearly WCF Data Services can do it. – Alex Dresko Feb 10 '12 at 19:54

2 Answers2

1

You can use following NuGet package to apply filter: https://www.nuget.org/packages/Community.OData.Linq

Code sample would be:

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();
Ihar Yakimush
  • 562
  • 4
  • 6
0

If you use the MVC wrapper for the igGrid then the mapping is done for you. The following is a quote from the Infragistics jQuery help:

"When the ASP.NET MVC wrapper is used to bind to server-side data through LINQ (IQueryable), all filtering information that’s encoded in the URL is automatically translated to LINQ expression clauses (Where clause), so you do not need to do anything additional in order to filter the data."

alhalama
  • 3,218
  • 1
  • 15
  • 21