I created a basic application using a WebAPI by following an online tutorial. It works fine for what it does. The GET method retrieves a list from a db table and populates it in the front end web site. I am simply trying to filter that list by passing one or more parameters.

This the code from the EmployeeController.cs file. Much of the code is unchanged from that created from the template. (sorry if it is too much but I wanted to provide sufficient info)

Can I achieve my goal by modifying the "return db.Employees;" line, or do I need to something more involved?

Thank you for your help!

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using WebAPI.Models;

namespace WebAPI.Controllers
{
    public class EmployeeController : ApiController
    {
        private DBModel db = new DBModel();

        // GET: api/Employee
        public IQueryable<Employee> GetEmployees()
        {
            //return db.Employees;
            return db.Employees;
        }


        // PUT: api/Employee/5
        [ResponseType(typeof(void))]
        public IHttpActionResult PutEmployee(int id, Employee employee)
        {

            if (id != employee.EmployeeID)
            {
                return BadRequest();
            }

            db.Entry(employee).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeeExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/Employee
        [ResponseType(typeof(Employee))]
        public IHttpActionResult PostEmployee(Employee employee)
        {
            db.Employees.Add(employee);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = employee.EmployeeID }, employee);
        }

        // DELETE: api/Employee/5
        [ResponseType(typeof(Employee))]
        public IHttpActionResult DeleteEmployee(int id)
        {
            Employee employee = db.Employees.Find(id);
            if (employee == null)
            {
                return NotFound();
            }

            db.Employees.Remove(employee);
            db.SaveChanges();

            return Ok(employee);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool EmployeeExists(int id)
        {
            return db.Employees.Count(e => e.EmployeeID == id) > 0;
        }
    }
}

2 Answers

0
Shamseer K On Best Solutions

In responds to your question in my youtube channel video here.
Suppose you want to filter employees with their employee code then api method will be like this.

public IQueryable<Employee> GetEmployees(string empCode)
{
    return db.Employees.Where(x => x.EMPCode==empCode);
}

Corresponding parameter can be passed through query string. like this : http://localhost:46568/api/employee?empCode=BJC


api method with multiple filter options

public IQueryable<Employee> GetEmployees(string empCode,string mobile)
{
    return db.Employees.Where(x => x.EMPCode == empCode || x.Mobile == mobile);
}

eg. request : http://localhost:46568/api/employee?empCode=BJC&mobile=754010

0
Sunil Dhappadhule On

OData Query options :- It can be helpful for you when the client sends some parameters in the request URI and those parameters are applied on the server side to perform the desired actions while getting data from API service interface.

[MyQueryable]  
public IQueryable<Employee> GetEmployees(ODataQueryOptions opts)  
{  
    if (opts.OrderBy != null)  
    {  
        opts.OrderBy.Validator = new MyOrderByValidator();  
    }  
    var settings = new ODataValidationSettings()  
    {  
    };  
    opts.Validate(settings);  
    IQueryable results = opts.ApplyTo(db.Employees.AsQueryable());  
    return results as IQueryable<Employee>;  
}