Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OData Unbound function send and receive primitive collection

Need an example on how to pass a collection of primitive type to an unbound function /or action and also how to return a collection of primitive type.
Such as a list or array of integers.
Here is a simple example.

List<int> GetEvenNumbers(List<int> numbers)
{
// loop through numbers collection and return a list of the even numbers
}

The following sites talk about using function / actions but does not explain passing / receiving collections.

http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/odata-actions-and-functions

https://aspnet.codeplex.com/sourcecontrol/latest#Samples/WebApi/OData/v4/

I even posted a suggestion on the "Show Me How With Code" but have not received a response.
http://aspnet.uservoice.com/forums/228522-show-me-how-with-code/suggestions/6264729-odata-v4-passing-collection-as-parameter-to-unbou

Here is some code that I have but it does not seem to work.

// in the controller
[HttpGet]
[ODataRoute("GetEvenNumbers(numbers={numbers})")]
public IHttpActionResult GetEvenNumbers(List<int> numbers)
{
    List<int> evenNumbers = new List<int>();
    foreach (var number in numbers)
    {
        if (number % 2 == 0)
        {
            evenNumbers.Add(number);
        }
    }
    return Ok(evenNumbers);
}

// in the WebApiConfig
var testCollectionFunction = builder.Function("GetEvenNumbers");
testCollectionFunction.CollectionParameter<int>("numbers");
testCollectionFunction.ReturnsCollection<int>();

In WCF this was very simple but in OData it is not so simple.

like image 884
goroth Avatar asked Aug 08 '14 16:08

goroth


2 Answers

As far as I know, webapi odata does not allow collections in Functions parameters. But you can use Actions as a workaround.

Model builder:

        var testCollectionFunction = modelBuilder.Action("GetEvenNumbers");
        testCollectionFunction.CollectionParameter<int>("numbers");
        testCollectionFunction.ReturnsCollection<int>();

Controller:

    [HttpPost]
    [ODataRoute("GetEvenNumbers")]
    public IHttpActionResult GetEvenNumbers(ODataActionParameters parameter)
    {
        IEnumerable<int> numbers = parameter["numbers"] as IEnumerable<int>;
        List<int> evenNumbers = new List<int>();
        foreach (var number in numbers)
        {
            if (number % 2 == 0)
            {
                evenNumbers.Add(number);
            }
        }
        return Ok(evenNumbers);
    }

Request:

POST http://localhost:44221/odata/GetEvenNumbers HTTP/1.1
Host: localhost:44221
Connection: keep-alive
Content-Length: 17

{"numbers":[1,2]}

Response:

HTTP/1.1 200 OK
Content-Type: application/json; odata.metadata=full
OData-Version: 4.0
Content-Length: 109

{
  "@odata.context":"http://localhost:44221/odata/$metadata#Collection(Edm.Int32)","value":[
    2
  ]
}
like image 144
Tan Jinfu Avatar answered Oct 15 '22 09:10

Tan Jinfu


So I wanted to provide and update to this post. I'm playing around with OData v4 Using Web API 2.2. I was able to get parameters passed to an unbound function.

Here is the builder code in my Register method.

ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Employee>("Employees");

//other stuff here
builder.Function("EmployeesWithParameters")
       .ReturnsCollectionFromEntitySet<Employee>("Employees")
       .CollectionParameter<string>("Ids");

Here is the actual method call on my controller.

    [HttpGet]
    [ODataRoute("EmployeesWithParameters(Ids={Ids})")]
    public IQueryable<Employee> EmployeesWithParameters([FromODataUri]string[] Ids)
    {
        IQueryable<Employee> result = db.Employees.Where(p => Ids.Contains(p.EmployeeId) );

        return result; ;
    }
like image 25
Cyrus Downey Avatar answered Oct 15 '22 08:10

Cyrus Downey