How can I create and initialize the following class? Specifically I'm struggling with TSortKey
. I thought it would be something like:
var p = new QueryParameters<Employees, e => e.LastName>(); // WRONG!!
public class QueryParameters<T, TSortKey> where T : class
{
public int Page { get; set; }
public int PageSize { get; set; }
public string Filter { get; set; }
public Func<T, TSortKey> SortSelector { get; set; }
public bool Ascending { get; set; }
}
What I'm trying to do is replace the GetEmployees
method arguments in the code below with the QueryParameters
object above.
public QueryResults<Employees> GetEmployees<TSortKey>(int page, int pageSize, string filter, Func<Employees, TSortKey> sortSelector, bool asc)
{
IEnumerable<Employees> query = Employees;
if (filter != "*")
{
query = query.Where (e => e.LastName.StartsWith(filter));
}
if (sortSelector != null)
{
query = asc ? query.OrderBy(sortSelector) : query.OrderByDescending(sortSelector);
}
var results = new QueryResults<Employees>
{
TotalItems = query.Count(),
Items = query.Skip((page - 1) * pageSize).Take(pageSize)
};
return results;
}
public class QueryResults<T> where T : class {
public IEnumerable<T> Items { get; set; }
public int TotalItems { get; set; }
}
I think I'm close but I might also be out in left field.