简体   繁体   English

如何上课并允许方法接受通用列表

[英]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.

相关问题 如何接受泛型类并使用其属性/方法 - How Can I Accept a Generic Class and Use Its Properties / Methods 如何将具有类名称的字符串转换为Generic将接受的类? - How can I convert a string with the name of a class into a class that a Generic will accept? 如何按主键或任何默认值排序,以使我可以在通用集合上跳过并接受EF? - How can I sort by primary key or any default value to allow me to Skip and Take in EF on a generic collection? 我如何在非泛型静态类的多个方法中使用同一泛型类型 - How can I use the same generic type in multiple methods in a non-generic static class 如何声明可以接受可为空 Guid 的泛型类型类? - How to declare a generic type class that can accept nullable Guid? 我怎样才能使这些方法通用? - how can I make these methods generic? 如何允许方法接受字符串或int? - How can I allow a method to accept either a string or int? C#如何允许C ++不允许虚拟模板方法的虚拟泛型方法? - How can C# allow virtual generic methods where C++ can't allow virtual template methods? 如何创建可以采用SOME基本类型的泛型类 - How to create a generic class that can take SOME primitive types 如何创建可以采用许多参数的通用 class - How to create a generic class that can take many parameters
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM