泛型C#组织的方法取决于类型

时间:2014-11-05 14:17:52

标签: c# generics nested-generics

我尝试使用基本方法创建一个基类,该方法使用依赖于Type的函数对List进行排序。

我的编译器显示此错误

错误13 Impossibile转换'System.Linq.Expressions.Expression< System.Func< MLOLPlus.Business.Dealer,string>>'在'System.Linq.Expressions.Expression< System.Func< T,string>>'中。 D:\ Documenti \ Lavori \ timage \ MLOLPlus \ src \ MLOLPlus.Business \ DataAcess \ DataTablesClasses \ DataTableMultiSort.cs 197 20 MLOLPlus.Business

IdentityEntity是所有自定义类数据类型

的基本抽象类库

示例:

  1. 用户继承IdentityEntity
  2. 编辑也是
  3. 基类MultiSort:

    public class DataTableMultiSort
    {
    
        public DataTableParameterModel DataTable { get; set; }
    
        public IQueryable<T> MultiSort<T>(IQueryable<T> basequery) where T : IdentityEntity{
            return CreateSortedQuery<T>(basequery, DataTable);
        }
    
        private IOrderedQueryable<T> CreateSortedQuery<T>(IQueryable<T> baseQuery, DataTableParameterModel parameterModel) where T : IdentityEntity
        {
            var orderedQuery = (IOrderedQueryable<T>)baseQuery;
    
            for (int i = 0; i < parameterModel.iSortingCols; ++i)
            {
                var ascending = string.Equals("asc", parameterModel.sSortDir[i], StringComparison.OrdinalIgnoreCase);
                int sortCol = parameterModel.iSortCol[i];
    
                Expression<Func<T, string>> orderByExpression = GetOrderingFunc<T>(sortCol);
                if (orderByExpression != null)
                {
                    ...do things
                }
                else
                {
                    if (ascending)
                    {
                        orderedQuery = (i == 0)
                            ? orderedQuery.OrderBy(c => c.Id)
                            : orderedQuery.ThenBy(c => c.Id);
                    }
                    else
                    {
                        ...
                    }
                }
    
            }
            return orderedQuery;
        }
    
        protected virtual Expression<Func<T, string>> GetOrderingFunc<T>(int ColumnIndex) where T : IdentityEntity
        {
            return null;
        }
    
    }
    

    自定义用户多重排序

      public class UserMultiSort : DataTableMultiSort
        {
            protected override Expression<Func<T, string>> GetOrderingFunc<T>(int ColumnIndex)
            {
                Expression<Func<User, string>> InitialorderingFunction;
                switch (ColumnIndex)
                {
                    case 1:
                        InitialorderingFunction = c => c.FirstName;
                        break;
                    case 2:
                        InitialorderingFunction = c => c.LastName;
                        break;
                    default:
                        InitialorderingFunction = null;
                        break;
                }
    
                return InitialorderingFunction;
            }
    
        }
    

    自定义编辑器Multisort

    public class EditorMultiSort : DataTableMultiSort
    {
        protected override Expression<Func<T, string>> GetOrderingFunc<T>(int ColumnIndex)
        {
            Expression<Func<Editor, string>> InitialorderingFunction;
            switch (ColumnIndex)
            {
                case 1:
                    InitialorderingFunction = c => c.BusinessName;
                    break;
                case 2:
                    InitialorderingFunction = c => c.Address;
                    break;
                default:
                    InitialorderingFunction = null;
                    break;
            }
    
            return InitialorderingFunction;
        }
    
    }
    

1 个答案:

答案 0 :(得分:3)

您收到编译器错误的原因是您已将通用约束推送到各个方法,而不是将限制放在上。您拥有它的方式,无法使用返回GetOrderingFunc<T>的方法覆盖内在泛型方法Expression<Func<User, string>>

如果您使DataTableMultiSort具有通用性并从方法中删除通用参数,那么您发布的内容有效:

public class DataTableMultiSort<T> where T : IdentityEntity
{
...
    protected virtual Expression<Func<T, string>> GetOrderingFunc(int ColumnIndex)
    {
        return null;
    }
}

public class UserMultiSort : DataTableMultiSort<User>
{
    protected override Expression<Func<User, string>> GetOrderingFunc(int ColumnIndex)
    {
        Expression<Func<User, string>> InitialorderingFunction;
        ...
     }
}