locked
IQueryable and odata filters not working RRS feed

  • Question

  • User-1177406051 posted

    I am using WebAPI and returning an IQueryable of an object.

    This is framework 4.5 and MVC4.

    My API call works fine, but it always returns 100 rows (the 'Take(100)' is being used to limit rows) even when I use a filter such as:

    ?$filter=Name eq 'Testing 123'

    or 'beginswith' or other fields...

    It does not matter, it just returns everything.

    I read that maybe I need to decorate with [Queryable] attribute, but I think that may be old and have been deprecated in newer version.

    Thanks.

    Thursday, February 7, 2013 5:09 PM

Answers

  • User-58016157 posted

    First change:

    // GET api/company
            [Queryable]
            public IQueryable<Models.DataModels.CompanyModel> Get()
            {
                return repository.GetCompanies().AsQueryable();
            }
    

    Also it could be that you return getCompanies().ToList(); The AsQueryable only converts an IEnumerable so the ToList() might be affecting it somehow.  

    Also try skipping your repo as a test and do a straight db.Companies.AsQueryable() in your api controller (you might need to change your filter for correct model property).  This will at least let you see if problem lies with that line or within the repo.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 8, 2013 11:54 AM

All replies

  • User-58016157 posted

    You are correct you do need to use the [Queryable] attribute, also try changing it to:

    $filter=substringof('Testing 123',Name) eq true



    Friday, February 8, 2013 10:17 AM
  • User-1177406051 posted

    That made no difference.  I still get the top 100 rows with no filter applied.

    Thanks for the reply.

    Friday, February 8, 2013 10:36 AM
  • User-58016157 posted

    Could you post the code for the Model and Contoller?

    Friday, February 8, 2013 10:58 AM
  • User-1177406051 posted
    API controller:
    
            // GET api/company
            public IQueryable<Models.DataModels.CompanyModel> Get()
            {
                return repository.GetCompanies().AsQueryable();
            }
    
    
    
    
    Repository methods:
    
            IEnumerable<DataModels.CompanyModel> XYZRepository.GetCompanies()
            {
                return getCompanies().ToList();
            }
    
            private IQueryable<Models.DataModels.CompanyModel> getCompanies()
            {
                var a = from b in db.Companies.Where(m=>m.Status =="ACTIVE")
                        select new Models.DataModels.CompanyModel
                        {
                             Description = b.CompanyDesc,
                               Industry = b.Industry,
                                Name = b.CompanyName
                        };
    
                return a;
            }

    Friday, February 8, 2013 11:02 AM
  • User-58016157 posted

    First change:

    // GET api/company
            [Queryable]
            public IQueryable<Models.DataModels.CompanyModel> Get()
            {
                return repository.GetCompanies().AsQueryable();
            }
    

    Also it could be that you return getCompanies().ToList(); The AsQueryable only converts an IEnumerable so the ToList() might be affecting it somehow.  

    Also try skipping your repo as a test and do a straight db.Companies.AsQueryable() in your api controller (you might need to change your filter for correct model property).  This will at least let you see if problem lies with that line or within the repo.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, February 8, 2013 11:54 AM