Duplicated Records Being Returned by Web API Method

I have created what I thought to be a simple controller selecting on a database view. However, the first record is returned multiple times in the JSON result. For example, when filtering using Odata, 7 different records are expected however, 7 of the same record are being returned.

Here is the controller code:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using MyAPI.WebAPI.Models;
using System.Web.OData;
using System.Security.Principal;
using System.Web;

namespace MyAPI.WebAPI.Controllers
{
    public class MyViewController : ApiController
    {
        private MyEntities db = new MyEntities();

        // GET
        [EnableQuery] // Enables OData query integration
        public IHttpActionResult GetMetrics()
        {
            return Ok(db.ViewResults.AsQueryable());
        }
    }
}

When using a breakpoint on the return statement, I can see where all 4000 results are being returned in the query and the OData seems to be filtering only the 7 records I need but when it is sent via JSON string it is duplicated.

Has anyone experienced anything like this?

Asp.Net Web Api trailing / causing odata query to fail

I am using HttpClient to query the WebApi controller using the below code:

        var uri = new Uri(url);
        var message = new HttpRequestMessage(method, uri);
        if (content != null) message.Content = new StringContent(content, Encoding.UTF8, "application/json");

        var client = new HttpClient();
        if (content != null) Debug.WriteLine($"Payload:\n {content}");
        var response = await client.SendAsync(message);
        return response;

The generated Url is as below:
http://localhost:9333/api/roles?$filter=%20Name%20eq%20’Some%20Role’/

Note the trailing ‘/’. This causes the OData Validation (code below) to fail:

    [HttpGet]
    [Route("")]
    public async Task GetRoles(ODataQueryOptions options)
    {
        GuardForODataException(options);

        var filteredResults = await _roleService.GetRolesAsync(options);
        return Ok(filteredResults);

    }

    public void GuardForODataException(ODataQueryOptions options)
    {
        try
        {
            var validationSettings = new ODataValidationSettings();
            options.Validate(validationSettings);
        }
        catch (Exception e)
        {

            var result =
                new ValidationFailedResult(
                    Request,
                        new ValidationResultModel(
                            new ValidationResult(
                                new List { new ValidationFailure("", "Odata Query is invalid") })));

            throw new HttpResponseException(result.ExecuteAsync(default(CancellationToken)).Result);
        }
    }

The exception I am getting is

“An identifier was expected at position 9.”

If however, I remove the trailing /, then the odata query returns the expected results.

I wasn’t able to find much on how to construct a HttpRequestMessage without the / at the end. Has anyone encountered this before?

Modify odata results after query

I have an ASP.NET Web API app using Entity Framework and Odata.

I would like to modify the results of a query when using a GET… currently, within the controller, you effectively just pass the EntityFramework data entities back to the Odata handlers…

[EnableQuery]
public IQueryable GetLineItem()
{
    return db.myEntities;
}

It’s simple to prepend whatever query Odata passes into this by simply returning a subset

return db.myEntity.Where(myEntity => myEntity.Name == "Bob")

Odata will add whatever is in the $filter querystring parameter to the expression passed in here and you get the subset of these results.

However, I would like to iterate over the results once the query executes and the SQL results are parsed into entity objects.

I have tried creating a wrapper that implements the IQueryable interface and hooking into the GetEnumerator methods and the same for the IProvider and hooking into the execute method. Odata doesn’t seem to be using either one of these.

Is there a way to do this?

Can ASP.NET Web API OData Return Non-Collection Properties?

I have the following class:

    public class WeeklyReport
    {
        public IEnumerable DailyReports { get; set; }

        public int? TotalReport
        {
            get
            {
                return DailyReports.Sum(x => x.ReportId);
            }
        }

        public string Title
        {
            get
            {
                return "Weekly Report";
            }
        }
    }

    public class DailyReport
    {
        public string Office { get; set; }
        public string Name { get; set; }
        public string Type { get; set; }
        public string ReportTo { get; set; }
        public DateTime Collection { get; set; }
        public int? Leads { get; set; }
    }

In one of my method in OData controller, I return IQueryable of WeeklyReport.
However, when the OData endpoint is queried, the returned JSON looks something like:

{
"odata.metadata": "http://localhost:546/odata/$metadata#WeeklyReports",
"value": [
    {
        "DailyReports": [
            {
                "Office": "002",
                "Name": "First Last",
                "Type": 10,
                "ReportTo": "00002",
                "Collection": "2014-03-18T00:00:00",
                "Leads": null
            },
            {
                "Office": "002",
                "Name": "Agent, ",
                "Type": 10,
                "ReportTo": "00002",
                "Collection": "2014-03-18T00:00:00",
                "Leads": null
            }
        ]
    },
    {
        "DailyReports": [
            {
                "Office": "002",
                "Name": "First Last",
                "Type": 10,
                "ReportTo": "00002",
                "Collection": "2014-03-18T00:00:00",
                "Leads": null
            },
            {
                "Office": "002",
                "Name": "Agent, ",
                "Type": 10,
                "ReportTo": "00002",
                "Collection": "2014-03-18T00:00:00",
                "Leads": null
            }
        ]
    }
]

Is there a way to set ASP.NET Web API OData to return WeeklyReport.TotalReport and WeeklyReport.Title as well?

Thanks!

OData queries and types other than IQueryable in ASP.NET Web API

I am building an ASP.NET Web API application that returns an Atom or an RSS feed. To do this, it builds a System.ServiceModel.Syndication.SyndicationFeed and a custom MediaTypeFormatter is responsible for handling the HTTP Accept Header, converting the SyndicationFeed to either an Atom10FeedFormatter or an Rss20FeedFormatter, and streaming the result to the response stream. So far, so good.

My controller looks something like this:

    public class FeedController : ApiController
    {
        public HttpResponseMessage Get()
        {
            FeedRepository feedRepository = new FeedRepository();
            HttpResponseMessage successResponseMessage = new HttpResponseMessage(feedRepository.GetSyndicationFeed());
            return successResponseMessage;
        }
    }

What I would like to do is make use of the built-in OData querying to filter my feed, but changing the return type of the Get() method to IQueryable obviously will not work since a SyndicationFeed does not implement IQueryable.

Is there a way to use the built in OData querying on the IEnumerable property on the SyndicationFeed?