We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Asp.net Core WebApi filtering/ordering

Hello.
I'm using Angular 2 grid control with an asp.net core web api project.
I managed to retrieve data using JsonResult on on controller and also to insert/update/delete objects.
But now I'm stuck on how to support ordering/filtering on GET. The querystring contains the string query ("name eq syncfusion") but how is it possible to translate to Where and query the EF context?
I saw some info about using "ODataQueryOptions" but there is no support for odata on asp.net core yet, as far as i know.

Angular component:
    this.datamanager = new ej.DataManager({
            url: '/api/Customers',
            adaptor: new ej.WebApiAdaptor()
        });

C# api controller action:

        // GET: api/Customers
        [HttpGet]
        public JsonResult Get() {
            var queryString = Request.Query;
            var skip = Convert.ToInt32(queryString["$skip"]);
            var take = Convert.ToInt32(queryString["$top"]);
            if (take <= 0) {
                take = 100;
            }
            var data = _context.Customers.ToList();
            return Json(new {result = data.Skip(skip).Take(take), count = data.Count()});
        }



Thanks in advance.

3 Replies

KR Keerthana Rajendran Syncfusion Team June 23, 2017 01:13 PM UTC

Hi Demetrios, 
 
Thank you for contacting Syncfusion support. 
 
Query: I saw some info about using "ODataQueryOptions" but there is no support for odata on asp.net core yet, as far as i know. 
 
Yes, ODataQueryOptions is not supported in Asp.Net core 
 
Query : But now I'm stuck on how to support ordering/filtering on GET. The querystring contains the string query ("name eq syncfusion") but how is it possible to translate to Where and query the EF context? 
 
We have prepared a sample based on your requirement. We suggest you to perform filtering manually as shown in the below code 
 
public object Get() 
                        { 
            var queryString = System.Web.HttpContext.Current.Request.QueryString; 
            var skip = Convert.ToInt32(queryString["$skip"]); 
            var take = Convert.ToInt32(queryString["$top"]); 
            string filter = queryString["$filter"]; 
            var data = db.Orders.Take(take).ToList(); 
             
            
            if (filter != null) 
            { 
                string key; 
                key = filter.Split(new string[] { "'" }, StringSplitOptions.None)[1]; 
               data= data.Where(fil => fil.CustomerID.Contains(key.ToUpper())).OrderBy(obj=>obj.ShipCity).Distinct().ToList(); 
 
            } 
 
            var count = data.Count; 
            return new { Items = data.Skip(skip).Take(take), Count = data.Count() }; 
        } 
 
In the above code we have filtered the Grid data based on CustomerID by getting the string entered and query the data using Where. We can order this data using OrderBy of Linq and get the resultant data displayed in Grid. 
 
Please refer to the below given sample 
 
 
 
If we have misunderstood your requirement please get back to us with the type of filter used in your sample and what type of ordering you are expecting so that we will analyze and provide better solution at the earliest 
 
Regards, 
Keerthana. 



DS Demetrios Seferlis June 23, 2017 01:48 PM UTC

Thank you Keerthana.

The problem with the solution you provide, is that filtering is going to be done on certain fields. Also what if we use excel like filtering where could be more than one filters on one field?

I don't see any way for simple web api usage. One would be to use .net framework that supports odata but the thing is that plans are the server to be running on linux so no luck :)...

I think I will try urladaptor by modifying the controller.

Thanks for your time anyway.



KV Karthikeyan Viswanathan Syncfusion Team June 26, 2017 12:45 PM UTC

Hi Demetrios,     
Thanks for the update. 
Actually, The queryString["$filter"] will return all the filtered columns with certain filter operation not a single. 
Please let us know if you need further assistance related to URL adaptor.  
 
Regards,   
Karthikeyan V. 
 


Loader.
Up arrow icon