[英]How can I take a class and allow the methods to accept a generic list
I would like to take the ResultSet class shown below and modify it so it can be generic. 我想采用下面显示的ResultSet类并对其进行修改,以便可以通用。 As you can see, it only accepts Product List but I would like it to take List-OneOfMyModels> or List-T> for the sake of code re-usability in my controllers.
如您所见,它只接受产品列表,但是我希望它采用List-OneOfMyModels>或List-T>,以便在控制器中重新使用代码。
public class ResultSet
{
public List<Product> GetResult(string search, string sortOrder, int start, int length, List<Product> dtResult, List<string> columnFilters)
{
return FilterResult(search, dtResult, columnFilters).SortBy(sortOrder).Skip(start).Take(length).ToList();
}
public int Count(string search, List<Product> dtResult, List<string> columnFilters)
{
return FilterResult(search, dtResult, columnFilters).Count();
}
private IQueryable<Product> FilterResult(string search, List<Product> dtResult, List<string> columnFilters)
{
IQueryable<Product> results = dtResult.AsQueryable();
results = results.Where(p => (search == null || (p.Name != null && p.Name.ToLower().Contains(search.ToLower()) || p.PublicDisplayNo != null && p.PublicDisplayNo.ToLower().Contains(search.ToLower())))
);
return results;
}
}
And in my controller I call the method like so: 在我的控制器中,我像这样调用该方法:
public JsonResult DataHandler(DTParameters param)
{
try
{
var dtsource = _context.Products.ToList();
List<String> columnSearch = new List<string>();
foreach (var col in param.Columns)
{
columnSearch.Add(col.Search.Value);
}
List<Product> data = new ResultSet().GetResult(param.Search.Value, param.SortOrder, param.Start, param.Length, dtsource, columnSearch);
int count = new ResultSet().Count(param.Search.Value, dtsource, columnSearch);
DTResult<Product> result = new DTResult<Product>
{
draw = param.Draw,
data = data,
recordsFiltered = count,
recordsTotal = count
};
return Json(result);
}
catch (Exception ex)
{
return Json(new { error = ex.Message });
}
}
This should get you going: 这应该可以帮助您:
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.ComTypes;
public class ResultSet
{
public List<T> GetResult<T>(string search, string sortOrder, int start, int length, List<T> dtResult, List<string> columnFilters) where T : class
{
return FilterResult(search, dtResult, columnFilters).OrderBy(sortOrder).Skip(start).Take(length).ToList();
}
public int Count<T>(string search, List<T> dtResult, List<string> columnFilters) where T : class
{
return FilterResult(search, dtResult, columnFilters).Count();
}
private IQueryable<T> FilterResult<T>(string search, List<T> dtResult, List<string> columnFilters) where T : class
{
IQueryable<T> results = dtResult.AsQueryable();
results = results.Where(p => (search == null || (p.Name != null && p.Name.ToLower().Contains(search.ToLower()) || p.PublicDisplayNo != null && p.PublicDisplayNo.ToLower().Contains(search.ToLower()))));
return results;
}
}
This won't support the cases where you are using properties on the generic type, T, because I have only typed it as a class. 这将不支持您在通用类型T上使用属性的情况,因为我只将其作为类输入了。 You can change the typing following the documentation here .
您可以按照此处的文档更改类型。 You'll want to type it with a base class or an interface to get things like
p.Name
to work. 您将需要使用基类或接口来键入它,以使
p.Name
类的东西可以正常工作。
You would have to make the whole generic like public class ResultSet<T>
or make the methods in the class have generic input and return types like so public List<T> GetResult(params)
. 您将必须使整个泛型像
public class ResultSet<T>
或者使类中的方法具有泛型输入和返回类型,如public List<T> GetResult(params)
。
But this introduces a new problem in the filter result method which uses the actual Products
class to perform search and filter by its properties. 但这在筛选结果方法中引入了一个新问题,该方法使用实际的
Products
类通过其属性执行搜索和筛选。 Use Reflections in c#, System.Reflection
to look-up class property values of generic types. Reflections in c#, System.Reflection
使用Reflections in c#, System.Reflection
查找泛型的类属性值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.