I'm building an API using Web API where i return database tables as JSON.
This is my controller method:
public List<ExampleDataDTO> ExampleData(int offset = 0, int limit = 0)
{
return DataAccessLayer.GetExampleData(offset, limit);
}
DataAccess method:
public List<ExampleDataDTO> GetExampleData(int offset, int limit)
{
using (var db = new Context())
{
db.Configuration.ProxyCreationEnabled = false;
var exampleQuery = db.example.AsEnumerable().Select(item =>
new ExampleDataDTO
{
//Selects
});
if(offset == 0 && limit == 0)
{
return exampleQuery .ToList();
}
else
{
return exampleQuery .Skip(offset).Take(limit).ToList();
}
}
}
Now the ExampleDataDTO contains over 100 files, and i want to be able to filter data on any field with the API.
Is there any way to make dynamic query parameters? for example if i go to localhost/api/ExampleData?offset=0&limit=10&name=test1&size=test2 i want to get the keys and values for name and size to be able to include them in my LINQ code.
Is this doable?
EDIT:
I can get all the parameters as IEnumerable<KeyValuePair<String, String>> queryString
Is there any way to use the Key in a lambda expression i were to loop through all the keyValuePairs? like Where(c = c.Key == Value),
foreach(var queryKeyValuePair in queryString)
{
Something including Where(c => c.queryKeyValuePair.Key == queryKeyValuePair.Value)
}
Skip().Take(). What is the list ordered by? Is it possible that the requirements might change to order it differently or to, worst case scenario, allow the user of the API to order it as they see fit?localhost/api/ExampeData?$filter=offset eq 0 and limit eq 10 and name eq test