ODataSamples
ODataSamples copied to clipboard
Query option apply
Hi I have a requirement of retrieving data from database dynamically based on API methods. For which i got the sample code from stack overflow for the same code mentioned in Github.
https://stackoverflow.com/questions/29981931/dynamic-odata-service-in-c-sharp-from-runtime-data-layer
But it doesn't show or suggest how to apply the Filter options.Sample code mentioned below
public EdmEntityObjectCollection Get() { // get Edm type from request ODataPath path = Request.ODataProperties().Path; // ODataProperties() needs using System.Web.OData.Extensions IEdmType edmType = path.EdmType;
IEdmCollectionType collectionType = (IEdmCollectionType)edmType;
IEdmEntityType entityType = (IEdmEntityType)collectionType.ElementType.Definition;
IEdmModel model = Request.ODataProperties().Model;
ODataQueryContext queryContext = new ODataQueryContext(model, entityType, path);
ODataQueryOptions queryOptions = new ODataQueryOptions(queryContext, Request);
// TODO: apply the query option on the IQueryable here.
// read all rows from table (could be optimized using query context)
var table = (DatabaseTable)ControllerContext.ControllerDescriptor.Properties["table"];
var cnx = (string)ControllerContext.ControllerDescriptor.Properties["connectionString"];
var data = ReadData(entityType, table, cnx);
var a = queryOptions.ApplyTo(data.AsQueryable());
return new EdmEntityObjectCollection(new EdmCollectionTypeReference(collectionType), data);
}
Any help on this will be much appreciated
Am curious about this as well.