title: How to Add OData $select functionality to WebAPI
body: <p>I am using WebApi v5 and OData v4.</p>
  
  <p>I have a controller which extends from ApiController with a method that polls a repository for a simple list of Categories.</p>
  
  <pre><code>public class ITSRController : ApiController
  {
      private IITSRRepository repository;
  
      public ITSRController(IITSRRepository itsrRepository)
      {
          repository = itsrRepository;
      }
  
      [Route("v1/request/categories")]
      [HttpGet]
      public IQueryable&lt;Category&gt; Categories()
      {
          return repository.Categories.AsQueryable();
      }
  </code></pre>
  
  <p>The Category class is a simple repository Entity class:</p>
  
  <pre><code>public class Category
  {
      public int Id { get; set; }
      public string CategoryName { get; set; }
  }
  </code></pre>
  
  <p>My WebApiConfig has the following code in it:</p>
  
  <pre><code>public static void Register(HttpConfiguration config)
  {
      config.MapHttpAttributeRoutes();
      config.AddODataQueryFilter();
  
      config.Routes.MapHttpRoute(
          name: "DefaultApi",
          routeTemplate: "itsrapi/{controller}/{id}",
          defaults: new { id = RouteParameter.Optional }
      );
  }
  </code></pre>
  
  <p>Without any additional code, I get the expected results for GET requests like:</p>
  
  <pre><code>v1/request/categories/?$orderby=CategoryName
  v1/request/categories/?$filter=CategoryName eq 'Cat1' or CategoryName eq 'Cat2'
  v1/request/categories/?$top=4&amp;$orderby=Id&amp;$skip=4
  </code></pre>
  
  <p>When I make the following GET request: 
  v1/request/categories/?$select=CategoryName</p>
  
  <p>I get the following exception:</p>
  
  <blockquote>
    <p>Object of type 'System.Linq.EnumerableQuery<code>1[System.Web.Http.OData.Query.Expressions.SelectExpandBinder+SelectSome</code>1[SWF.ITSR.Domain.Entities.Category]]'
    cannot be converted to type 'System.Collections.Generic.IEnumerable`1[SWF.ITSR.Domain.Entities.Category]'.</p>
  </blockquote>
  
  <p>After some research I changed my Categories method in my controller:</p>
  
  <pre><code>[Route("v1/request/categories")]
  [HttpGet]
  [EnableQuery]
  public IQueryable&lt;Category&gt; Categories(ODataQueryOptions options)
  {
      if (options.SelectExpand != null)
      {
          Request.ODataProperties().SelectExpandClause = options.SelectExpand.SelectExpandClause;
      }
  
      return repository.Categories.AsQueryable();
  }
  </code></pre>
  
  <p>When I call $select after this change I get the same error as above.</p>
  
  <p>I've tried modifying the method to the following:</p>
  
  <pre><code>public IEnumerable&lt;Category&gt; Categories(ODataQueryOptions options)
  {
      ...
      return repository.Categories.ToList();
  }
  </code></pre>
  
  <p>and I get a slightly different exception when calling $select</p>
  
  <blockquote>
    <p>Unable to cast object of type 
    'System.Linq.EnumerableQuery<code>1[System.Web.Http.OData.Query.Expressions.SelectExpandBinder+SelectSome</code>1[SWF.ITSR.Domain.Entities.Category]]' 
    to type 'System.Collections.Generic.IEnumerable`1[SWF.ITSR.Domain.Entities.Category]'.</p>
  </blockquote>
  
  <p>which may be semantically the same as the first error (perhaps failing somewhere else)</p>
  
  <p>I spent a good many hours researching how to add $select (and $expand) functionality to my WebApi controller to no avail. I have tried countless posted suggestions, and still the answer eludes me. I would appreciate any help.</p>
  
  <p>Thanks,</p>
  
  <p>Ken</p>