我正在尝试使用基方法创建一个基类,该基方法对具有依赖于类型的函数的列表进行排序。
我的编译器显示此错误
错误 13 无法在“System.Linq.Expressions.Expression<System.Func<T,string>>”中转换“System.Linq.Expressions.Expression<System.Func<MLOLPlus.Business.Dealer,string>>”。D:\Documenti\Lavori\timage\MLOLPlus\src\MLOLPlus.Business\DataAcess\DataTablesClasses\DataTableMultiSort.cs 197 20 MLOLPlus.Business
IdentityEntity 是所有自定义类数据类型的基础抽象类基础
例子:
- 用户继承 IdentityEntity
- 编辑也是
基类多排序:
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;
}
}
自定义编辑器
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;
}
}