Using the ODATA $ expand query option using WebAPI and ViewModel

This question is very similar, but does not give me what I need.

I am using Entity Framework 6. My database consists of two tables: Customer and CustomerTypes. I created a ViewModel for everyone. A client can be of the type:

public class Customer
{
    public int CustomerID { get; set; }
    public string CustomerName { get; set; }
    public CustomerTypeViewModel CustomerType { get; set; }
}

public class CustomerTypeViewModel
{
    public int CustomerTypeID { get; set; }
    public string CustomerTypeDescription { get; set; }
}

I have a Customer controller that provides an odata action method with an IQueryable return type:

[HttpPost, Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
    public IQueryable<CustomerViewModel> GetCustomersMatchingCriteria([FromBody]ODataActionParameters parameters)
    {
        var criteria = (CustomerMassUpdateCriteriaViewModel)parameters["Criteria"];

        return Common.Customers.GetCustomerMassUpdateCriteriaResults(criteria,
            ConfigurationManager.AppSettings["CLIENT_ID"]).Select(
            c => new CustomerViewModel()
            {
                CustomerID = c.CustomerID,
                CustomerName = c.CustomerName,
                CustomerType = new CustomerTypeViewModel() 
                {
                    CustomerTypeDescription = c.CustomerType.CustomerTypeDescription
                }
            });
    }

The Common.Customers.GetCustomerMassUpdateCriteriaResults method returns only the IQueryable client, which is the actual object.

The problem is calling this controller method with the following query string parameters:

$expand=CustomerType
$select=CustomerID,CustomerName,CustomerType/CustomerTypeDescription

This is an exception:

ObjectContent`1 "/JSON; = UTF-8'," ":" System.InvalidOperationException"

DbIsNullExpression , .

$expand CustomerType/CustomerTypeDescription $select .

, - . ?

1st EDIT:

ToList() IEnumerable, IQueryable, CustomerType, ODATA $ . , ODATA?

+4
1

:

ObjectContent`1 'application/json; = UTF-8', "": "System.InvalidOperationException"

, OData. OData . GlobalConfiguration.Configuration.EnableOData() RouteConfig.RegisterRoutes WebApiConfig.Register global.asax .

0

All Articles