title: OData order by navigation property
body: <p>I'm trying to consume <a href="http://services.odata.org/Northwind/Northwind.svc" rel="nofollow">Northwind</a> OData service in ASP.NET MVC project and using WebGrid controll for vizualizing. I create service reference with Visual Studio 2012 using service URL. Problem is in sorting of WebGrid. When I try to sort it on a navigational field, like CategoryName I get an error: "No property or field 'CategoryName' exists in type 'Product'". I understand the error, but I don't know how to make that work and get a needed portion of data. Any help will be appreciated.
  Controller:</p>
  
  <pre><code>private ODataNorthwind.NorthwindEntities context = new ODataNorthwind.NorthwindEntities(new Uri("http://services.odata.org/Northwind/Northwind.svc"));
      //
      // GET: /Odata/
  
      public ActionResult WebGrid(int page = 1, int rowsPerPage = 10, string sort = "ProductID", string sortDir = "ASC")
      {
          var data = GetProducts(page, rowsPerPage, sort, sortDir);
          return View(data);
      }
  
      // Data for WebGridObj
      private WebGridViewModel GetProducts(int page = 1, int rowsPerPage = 10, string sort = "ProductID", string sortDir = "ASC")
      {
          var query = context.Products.OrderBy(sort + " " + sortDir)
              .Select(p =&gt; new
              {
                  p.ProductID,
                  p.ProductName,
                  p.Category.CategoryName,
                  p.Supplier.CompanyName,
                  p.Supplier.Country
              });
  
          WebGridViewModel model = new WebGridViewModel
          {
              TotalRows = query.Count(),
              Products = query.Skip((page - 1) * rowsPerPage).Take(rowsPerPage).ToList().Select(p =&gt;
                 new HtmlViewModel
                 {
                     ProductId = p.ProductID,
                     ProductName = p.ProductName,
                     CategoryName = p.CategoryName,
                     CompanyName = p.CompanyName,
                     Country = p.Country
                 })
          };
          return model;
      }
  </code></pre>
  
  <p>View:</p>
  
  <pre><code>@{
      ViewBag.Title = "WebGrid";
      Layout = "~/Views/Shared/_Layout.cshtml";
  }
  
  &lt;h2&gt;OData WebGrid&lt;/h2&gt;
  
  &lt;div&gt;
  @{
      var grid = new WebGrid(null, defaultSort: "ProductId");
      grid.Bind(Model.Products, rowCount: Model.TotalRows, autoSortAndPage: false);
  }
  @grid.GetHtml(columns: grid.Columns(
      grid.Column("ProductId"),
      grid.Column("ProductName"),
      grid.Column("CategoryName"),
      grid.Column("CompanyName"),
      grid.Column("Country")
      ))
  &lt;/div&gt;
  </code></pre>