I'm using the OData query functionality for my single page application. I've recently tried to add some filtering in the business layer based on user permissions and it's causing my odata query to fail in certain situations.
My OData controller references a class in my business layer which returns a IQueryable. The business layer references an entity framework database first model.
In my business layer I've got the following property.
public IQueryable<Job> Jobs
{
get
{
IQueryable<Job> queryable = UnitOfWork.Jobs.AsExpandable();
var predicate = PredicateBuilder.False<Job>();
foreach (int clientID in userService.GetAllowedClientIds())
{
int temp = clientID;
predicate = predicate.Or(p => p.ClientId == temp);
}
return queryable.Where(predicate);
// I've tried returning this as .AsExpandable() again and as .AsQueryable() and neither change the result.
}
}
My odata controller is a simple pass through to the BL.
[Queryable(MaxExpansionDepth = 3)]
public IQueryable<Job> Get()
{
return jobService.Jobs;
}
Now depending on what query parameters I pass through in the URL I either get data or an "Cannot compare elements of type 'System.Collections.Generic.ICollection
1[[AssociatedType]]'. Only primitive types, enumeration types and entity types are supported."`
For example /Jobs?$expand=LatestJobAllocations/Gang
works perfectly but /Jobs?$expand=JobLocationStructures
fails. The difference is that LatestJobAllocations
is a view which I've added to the model and JobLocationStructures
is a table, also Job
to LatestJobAllocations
is a 1-0..1 relationship.
If I add paging to the request that fails it miraculously works. /Jobs?$expand=JobLocationStructures&$inlinecount=allpages&$top=25&$skip=0
which would be great except I can't use paging to get a single object /Jobs(1234)?$expand=JobLocationStructures&$inlinecount=allpages&$top=25&$skip=0
fails because paging isn't allowed on single entity endpoints.