Using $select in ASP.Net Web API2 OData

Introduction

This article explains how to use $select in ASP.NET Web API OData. Here we install the ASP.NET Web API2 OData Package. By using the $select we can find specified data from the controller.

$select: By using this query an option is provided to the user for selecting the desired set of information. It decides what field is to be returned to the user. When we use the $select operator it allows the user to create the subset of the entities and selects that paricualr set of data.

 Let's see an example.

Step 1

Create an application using the Web API 2.

  • Start Visual Studio 2013.
  • From the Start Window select "New Project".
  • Select "Installed" -> "Templates" -> "Visual C#" -> "Web" and select ASP.NET Web Application.

    Select Web Application

  • Click on the "OK" button.
  • From the ASP.NET project window select "Web API".

    Select Web API

  • Click on the "Create Project" button.

Step 2

Add a Model Class.

  • In the "Solution Explorer".
  • Right-click on the Model Folder.
  • Select "Add" -> "Class".
  • Select "Installed" -> "Visual C#" and select "Class".

    Add Model Class

  • Click on the "OK" button.

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. namespace WebApplication3.Models  
  6. {  
  7.     public class Student  
  8.     {  
  9.         public int ID { getset; }  
  10.         public string Name { getset; }  
  11.         public string Address { getset; }  
  12.         public int Marks { getset; }  
  13.     }  
  14. }   

Step 3

Install the "ASP.NET WebAPI2 OData" package.

  • Go to the Tools menu.
  • Select "Library Package Manager" -> "Manage NuGet Packages For Solutions".
  • Now open a Package window.

    Install OData PAckage

  • In the search box type "ASP.NETWebAPIOData" and install it.

Step 4

Create a Web API2 Controller.

  • In the "Solution Explorer".

  • Right-click on the "Controller" folder.

  • Select the "Controller" and from the controller window select "Common" -> "Web API".

    Select Web API2 Controller

  • Select the "Web API2 Empty Controller" and click on the "Add" button.

    Change Name

  • Change the name and click on the "Add" button.

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. using System.Web.Http.OData.Query;  
  8. using WebApplication3.Models;  
  9. namespace WebApplication3.Controllers  
  10. {  
  11.     public class StudentsController : ApiController  
  12.     {  
  13.         Student[] student = new Student[]  
  14.         {  
  15.             new Student{ID=1,Name="Student1",Address="Address1",Marks=34},  
  16.             new Student {ID=2,Name="Student2",Address="Address2",Marks=67},  
  17.             new Student {ID=3,Name="Student3",Address="Address3",Marks=89},  
  18.             new Student {ID=4,Name="Student4",Address="Address3",Marks=90}  
  19.         };  
  20.         // GET api/values  
  21.         [Queryable()]  
  22.         public IEnumerable<Student>Get()  
  23.         {  
  24.             return student;  
  25.         }  
  26.     }  
  27. } 

Step 5

Now execute the application and copy the URL. 

Copy the port fom here

Then open Fiddler.

  • Click on the Composer Tab.
  • Paste in the copied URL.
  • Click on the "Execute button".
  • The output display like this.

    Select the Information

  • Now we use the $select command in the URL; the URL looks like this: "http://localhost:5473/api/students?$select=ID,Name".

    Use $select Cmmand

It selects only the ID and Name of the Student.


Similar Articles