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.
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

Asp.net Core WebApi filtering/ordering

Thread ID:

Created:

Updated:

Platform:

Replies:

131139 Jun 22,2017 07:21 PM UTC Jun 26,2017 12:45 PM UTC Angular 3
loading
Tags: ejDatamanager
Demetrios Seferlis
Asked On June 22, 2017 07:21 PM UTC

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.

Keerthana Rajendran [Syncfusion]
Replied On 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. 


Demetrios Seferlis
Replied On 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.


Karthikeyan Viswanathan [Syncfusion]
Replied On 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. 
 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

;