|
public PageResult<Book> Get(ODataQueryOptions opts)
{
var results = _db.Books.AsQueryable();
var count = results.Count();
if (opts.OrderBy != null)
results = opts.OrderBy.ApplyTo(results);
if (opts.Filter != null)
{
results = opts.Filter.ApplyTo(results, new ODataQuerySettings()).Cast<Book>();
}
var queryString = opts.Request.Query;
string search = queryString["$search"];
if (search != null)
{
//sarch query is maintained. to oercome that we have used below workaround
string key = search.Split(" OR ")[search.Split(" OR ").Length - 1];
//searched the typed string using where query and retured the results.
results = results.Where(fil => fil.Id.ToString().Contains(key) || fil.Name.Contains(key) || fil.Gender.ToString().Contains(key) || fil.Active.ToString().Contains(key) || fil.CreditLimit.ToString().Contains(key) || fil.RegistrationDate.ToString().Contains(key));
}
if (opts.Skip != null)
results = opts.Skip.ApplyTo(results, new ODataQuerySettings());
if (opts.Top != null)
results = opts.Top.ApplyTo(results, new ODataQuerySettings());
if(search != null)
{
// to remove the QueryValidation for $search as it is not supported by ASP.NEt Core
opts.Request.Query = new QueryCollection();
}
return new PageResult<Book>(results, null, count);
}
|
|
// GET api/values
[HttpGet]
public PageResult<Book> Get(ODataQueryOptions opts)
{
var results = _db.Books.AsQueryable();
var count = results.Count();
if (opts.OrderBy != null)
results = opts.OrderBy.ApplyTo(results);
if (opts.Filter != null)
{
results = opts.Filter.ApplyTo(results, new ODataQuerySettings()).Cast<Book>();
}
var queryString = opts.Request.Query;
string search = queryString["$search"];
if (search != null)
{
//sarch query is maintained. to oercome that we have used below workaround
string key = search.Split(" OR ")[search.Split(" OR ").Length - 1];
//searched the typed string using where query and retured the results.
results = results.Where(fil => fil.Id.ToString().Contains(key) || fil.Name.Contains(key) || fil.Gender.ToString().Contains(key) || fil.Active.ToString().Contains(key) || fil.CreditLimit.ToString().Contains(key) || fil.RegistrationDate.ToString().Contains(key));
}
if(opts.Count != null)
count = results.Count();
if (opts.Skip != null)
results = opts.Skip.ApplyTo(results, new ODataQuerySettings());
if (opts.Top != null)
results = opts.Top.ApplyTo(results, new ODataQuerySettings());
return new PageResult<Book>(results, null, count);
}
|
|
|