In my OData controller, I am using the generic repository found here and can do the following:
[Queryable]
public Task<IQueryable<ContentType>> Get()
{
var result = _repository.Query().GetAsync();
return result;
}
As I would like to add some conditions to the query, such as UserId
and use the UnitOfWork
pattern,
I'm trying the following:
[Queryable]
public Task<IQueryable<ContentType>> Get()
{
// mock
var userId = 111;
var unitOfWork = new Repository.UnitOfWork(_db);
var result = unitOfWork.Repository<ContentType>().Query().Get()
.Where(u => u.UserId == userId).Cast<ContentType>() // <-- how to access .GetAsync()
return result;
}
I have tried the following (which works), but wondering if this is in fact the best way of accomplishing this?
[Queryable]
public async Task<IQueryable<ContentType>> Get()
{
// mock
var userId = 102;
var unitOfWork = new Repository.UnitOfWork(_db);
var result = unitOfWork.Repository<ContentType>().Query().Get()
.Where(u => u.UserId == userId).Cast<ContentType>().AsQueryable();
return await Task.Factory.StartNew(() => result);
}
-- Update --
Based on the discussion, I have removed the async portion of this and done the following:
public IEnumerable<ContentType> Get(ODataQueryOptions<ContentType> options)
{
var unitOfWork = new Repository.UnitOfWork(_db);
// mock
var userId = 102;
var contentTypes = options.ApplyTo(unitOfWork.Repository<ContentType>().Query().Get()
.Where(u => u.UserId == userId)
.OrderBy(c => c.Description))
.Cast<ContentType>().ToList();
return contentTypes;
}
I'm not stuck on doing this async, and it doesn't seem like there is a direct way to .GetAsync()
from IQueryable<TEntity>
(as defined in IReqpositoryQuery.cs), but curious to know of a proper way of accomplishing this.