我需要编写一些像这样的通用搜索方法:
public List<T> Search<T>(SearchParamsBase searchParams)
{
using (var context = new TestEntities())
{
var dataType = TypeMap.Get(typeof (T));
var dataSet = context.Set(dataType);
var searchQuery = CreateQuery((IEnumerable<object>) dataSet), searchParams)
return searchQuery.ToList()
}
}
我有一个CreateQuery()
应该过滤IEnumerable
对象的函数。这个函数对于所有类都是不同的。例如:
CreateQuery(IEnumerable<object> collection, SearchParamsBase searchParams)
{
var search = (SomeSearchImplementation)searchParams;
// filter
collection = collection.Where(x => x.Name == search.Name);
// select page
collection = collection.Skip(search.Page * search.CountPerPage);
collection = collection.Take(search.CountPerPage);
// order by and so on
// ...
return collection;
}
我怎样才能正确地实现这个想法?