0
votes

Following code converts the ViewModel query to model and then converts the returned result back to ViewModel as PageResult. All this works fine but when I try to use include as part of my default query(or even with the latest version as part of querycontext) then OData formatter plays funny and doesn't include child elements. I have debugged and confirmed that it actually contains child elements. This only happens for controllers that I extended from ODataController(so basically for the ones that are extended from ApiController all works fine but i need results in OData format). Please note that I have also tried with the latest nightly build(Microsoft.Data.OData 5.5.0.0) and still it doesn't work for me. Any help would highly be appreciated.

public class ProductsController : ODataController
    {
        APPContext context = new APPContext();

        public PageResult<ProductViewModel> Get(ODataQueryOptions QueryOptions)
        {
            EdmModel model = new EdmModel();
            ODataQueryContext queryContext = new ODataQueryContext(model.GetEdmModel(), typeof(Product));
            var mappedQuery = new ODataQueryOptions(queryContext, QueryOptions.Request);
            var results = new List<ProductViewModel>();

            foreach (var result in mappedQuery.ApplyTo(this.context.Serials.Include("Status").Include("Category")))
            {
                AutoMapper.Mapper.CreateMap(result.GetType(), typeof(ProductViewModel));
                results.Add(AutoMapper.Mapper.Map<ProductViewModel>(result));
            }

            PageResult<ProductViewModel> pr = new PageResult<ProductViewModel>(results.AsEnumerable<ProductViewModel>(), mappedQuery.Request.GetNextPageLink(), mappedQuery.Request.GetInlineCount());
            return pr;
        }
    }
1

1 Answers

1
votes

In OData related entities are represented as navigation links. So, if you have a customers feed, the related orders for each customer will not be part of the customers feed. Instead, they would be represented as navigation links. You can explicitly tell the OData service to expand the related entities using the $expand query option. So, if you want the related orders for each customer to be expanded, you should ask for the url ~/Customers?$expand=Orders.