WCF Web api and complex types with odata

Topics: Web Api
Jan 5, 2012 at 1:57 PM

I'm using WCF web api to bui;d a rest service that returns coplex objects from an odata query. Unfortunately, querying complex types doesn't seem to work. E.G.

public IQueryable<Person> Get()
{
    var people = new List<Person>()
    {
        new Person {
            Name="John", 
            Department = new Department{Id=2, Description="Lion Swaddling"}
        },
        new Person {
            Name="Jane", 
            Department = new Department{Id=4, Description="Face Surgery"}
        },
    };

    return people.AsQueryable();
}

The following uri returns nothing. http://localhost/api/people?$filter=Department/Id%20eq%20'2'

Does the Web Api, in fact, support querying complex types? And if so, is there something special i must to to enable it?

Coordinator
Jan 5, 2012 at 8:16 PM

Looks like your query is specifying the ID value as a literal string: $filter=Department/Id%20eq%20'2'

Should this be an integer value instead? Does this work?: $filter=Department/Id%20eq%202

Jan 5, 2012 at 8:20 PM
danroth27 wrote:

Looks like your query is specifying the ID value as a literal string: $filter=Department/Id%20eq%20'2' 

Should this be an integer value instead? Does this work?: $filter=Department/Id%20eq%202

This is just an example. My real service call is on a string field. It works just fine for primitive and string fields. Multivalued fields dont seem to work.

Jan 5, 2012 at 10:04 PM

Can you please share the class definition for Person ?

Jan 5, 2012 at 10:18 PM
raghuramn wrote:

Can you please share the class definition for Person ?

 

public class Person{
    public string Name{get; set;}
    public Department Department{get; set;}
}

public class Department{
    public int Id{get; set;}
    public string Description{get; set;}
}
Jan 6, 2012 at 3:13 AM

Resolved: Turns out some of my objects had null Description values. Once i made sure all had values it worked fine.