Queries and OData Types Other Than IQueryable in ASP.NET Web Interface

I am creating an ASP.NET web API application that returns an Atom or RSS feed. To do this, it creates System.ServiceModel.Syndication.SyndicationFeedand the user MediaTypeFormatteris responsible for processing the HTTP Accept header, converting SyndicationFeedto Atom10FeedFormatteror Rss20FeedFormatter, and streaming the result into a response stream, So far so good.

My controller looks something like this:

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

What I would like to do is use the OData built-in query to filter my channel, but changing the return type of the method Get()to IQueryable<SyndicationFeed>, obviously, will not work, because it SyndicationFeeddoes not work to implement IQueryable.

Is there a way to use the OData inline query in a property IEnumerable<SyndicationItem>on SyndicationFeed?

+5
source share
3 answers

The System.Linq namespace provides the AsQueryable extension method for the interface IEnumerable. Your code will look like this:

public class FeedController : ApiController
{
    public IQueryable<SyndicationFeed> Get()
    {
        FeedRepository feedRepository = new FeedRepository();

        //TODO: Make sure your property handles empty/null results:
        return feedRepository.GetSyndicationFeed()
                   .YourEnumerableProperty.AsQueryable();
    }
}
+3
source

This question is no longer relevant as Microsoft removes the rudimentary support for OData requests that were in the beta version of the web API.

OData. , CodePlex NuGet, : http://blogs.msdn.com/b/alexj/archive/2012/08/15/odata-support-in-asp-net-web-api.aspx

+3

You do not need to return IQuerable from the controller when working with OData. See https://docs.microsoft.com/en-us/aspnet/web-api/overview/odata-support-in-aspnet-web-api/supporting-odata-query-options

check the box "Call query parameters directly"

In your case, it will look like this:

public SyndicationFeed Get(ODataQueryOptions<SyndicationItem> opts)
{
    var settings = new ODataValidationSettings();

    opts.Validate(settings);

    SyndicationFeed result = feedRepository.GetSyndicationFeed();

    result.Items = opts.ApplyTo(result.Items.AsQuerable()).ToArray();

    return result;
}
0
source

All Articles