I have an ASP.Net Core RESTful API with an End-point that returns a IQueryable<dynamic>
type.
If OData's $filter
or $select
is not used in the URI, the data is returned correctly.
Because of the dynamic nature of the data, I cannot strongly type the endpoints result.
Is there a way to use OData's $filter
and $select
here?
My API is using the Dapper ORM. I am considering converting over to Entity Framework Core since it works very well with OData. But, if I can continue to use the Dapper ORM, it would save me refactoring a lot of code.
The application uses a lot of complex SQL statements that I would like to continue to use to avoid further refactoring.
If I build a "Business Object" for each of my complex queries, Dapper works fine with OData. The only draw back is that the data is flattened. This means the programmer of the Client consuming the API cannot implement OData's $expand
statement. But, we can get by with flattened master/detail data if we need to.
So, in my attempt to build an end-point for each of these complex queries, I decided to try and return an IEnumerable
or IQueryable
(both work) of the dynamic type so I would not have to write a Business Object (or Model) for each of our complex queries.
The .NET Core CLR accepts the dynamic type results and converts it to JSON successfully for the most part, except for $expand
as noted above.
Sorry for the late response, but I had to re-write one of my end-points so our real logic was not exposed. I kept the nature of what I'm trying to do in a generic way. My Repository object is just the Dapper ORM shrink-wrapped for our business purposes. Thanks in advance for your help.
[HttpGet("[action]")]
[EnableQuery]
public ActionResult<IQueryable<dynamic>> GetComplex()
{
string sql = "SELECT master.*, detail1.*, detail2.* " +
"FROM MasterTable AS master " +
"LEFT JOIN FirstDetailTable AS detail1 " +
" ON master.Id = detail1.MasterId " +
"LEFT JOIN SecondDetailTable AS detail2 " +
" ON master.Id = detail2.MasterId " +
"ORDER BY master.SomeColumn";
string myDB_CatalogName = "MyCatalog";
int commandTimeOut = 30;
Repository<dynamic> repository = new Repository<dynamic>(_dbConnectionString, myDB_CatalogName, commandTimeOut);
IQueryable<dynamic> myQueryResults = repository.ExecuteSqlStatement(sql).AsQueryable();
return Ok(myQueryResults);
}
$filter
manually – Chris Schaller Jul 29 '20 at 23:19