If you are using OData controller without using [EnableQuery] attribute, Below example help you to get all your odata query
Odata Query
/odata/GetTestData?$format=json&$filter=(Name+eq+'santosh'+AND+Age+eq+30)&$select=Name,Address&$top=50&$count=true
Step 1:
declare temp variable and load data from your repository like below:
IQueryable tempQuery = repo.GetTestData();
IQueryable result = tempQuery;
Step 2:
Add below lines of code to apply your query.
for $count- get count() from temp result variable so that you will get all data count
if (opts.Filter != null){
tempQuery = opts.Filter.ApplyTo(tempQuery, new ODataQuerySettings()) as Queryable;
}
if (opts.Top != null){
tempQuery = opts.Top.ApplyTo(tempQuery, new ODataQuerySettings()) as IQueryable;
}
if (opts.Skip != null){
tempQuery = opts.Skip.ApplyTo(tempQuery, new ODataQuerySettings()) as IQueryable;
}
if (opts.OrderBy != null){
tempQuery = opts.OrderBy.ApplyTo(tempQuery, new ODataQuerySettings()) as Queryable;
}
if (opts.SelectExpand != null){
Request.ODataProperties().SelectExpandClause = opts.SelectExpand.SelectExpandClause;
}
if (opts.Count != null)
{
Request.ODataProperties().TotalCount = result.Count();
}
Step 3:
Final full Odata Controller code
//Web API Odata Controller
[HttpGet]
[ODataRoute("GetTestData")]
public IQueryable GetTestData(ODataQueryOptions opts)
{
var repo = unitOfWork.TestRepository;
IQueryable tempQuery = repo.GetTestData();
IQueryable result = tempQuery;
if (opts.Filter != null){
tempQuery = opts.Filter.ApplyTo(tempQuery, new ODataQuerySettings()) as IQueryable;
}
if (opts.Top != null){
tempQuery = opts.Top.ApplyTo(tempQuery, new ODataQuerySettings()) as IQueryable;
}
if (opts.Skip != null){
tempQuery = opts.Skip.ApplyTo(tempQuery, new ODataQuerySettings()) as IQueryable;
}
if (opts.OrderBy != null){
tempQuery = opts.OrderBy.ApplyTo(tempQuery, new ODataQuerySettings()) as IQueryable;
}
if (opts.SelectExpand != null){
Request.ODataProperties().SelectExpandClause = opts.SelectExpand.SelectExpandClause;
}
if (opts.Count != null)
{
Request.ODataProperties().TotalCount = result.Count();
}
result = tempQuery.ToList().AsQueryable();
return result;
}
Happy coding👍
Odata Query
/odata/GetTestData?$format=json&$filter=(Name+eq+'santosh'+AND+Age+eq+30)&$select=Name,Address&$top=50&$count=true
Step 1:
declare temp variable and load data from your repository like below:
IQueryable
IQueryable
Step 2:
Add below lines of code to apply your query.
for $count- get count() from temp result variable so that you will get all data count
if (opts.Filter != null){
tempQuery = opts.Filter.ApplyTo(tempQuery, new ODataQuerySettings()) as Queryable
}
if (opts.Top != null){
tempQuery = opts.Top.ApplyTo(tempQuery, new ODataQuerySettings()) as IQueryable
}
if (opts.Skip != null){
tempQuery = opts.Skip.ApplyTo(tempQuery, new ODataQuerySettings()) as IQueryable
}
if (opts.OrderBy != null){
tempQuery = opts.OrderBy.ApplyTo(tempQuery, new ODataQuerySettings()) as Queryable
}
if (opts.SelectExpand != null){
Request.ODataProperties().SelectExpandClause = opts.SelectExpand.SelectExpandClause;
}
if (opts.Count != null)
{
Request.ODataProperties().TotalCount = result.Count();
}
Step 3:
Final full Odata Controller code
//Web API Odata Controller
[HttpGet]
[ODataRoute("GetTestData")]
public IQueryable
{
var repo = unitOfWork.TestRepository;
IQueryable
IQueryable
if (opts.Filter != null){
tempQuery = opts.Filter.ApplyTo(tempQuery, new ODataQuerySettings()) as IQueryable
}
if (opts.Top != null){
tempQuery = opts.Top.ApplyTo(tempQuery, new ODataQuerySettings()) as IQueryable
}
if (opts.Skip != null){
tempQuery = opts.Skip.ApplyTo(tempQuery, new ODataQuerySettings()) as IQueryable
}
if (opts.OrderBy != null){
tempQuery = opts.OrderBy.ApplyTo(tempQuery, new ODataQuerySettings()) as IQueryable
}
if (opts.SelectExpand != null){
Request.ODataProperties().SelectExpandClause = opts.SelectExpand.SelectExpandClause;
}
if (opts.Count != null)
{
Request.ODataProperties().TotalCount = result.Count();
}
result = tempQuery.ToList().AsQueryable();
return result;
}
Happy coding👍