I'm Tryng to made a base class with a base method that order a List with a function that depends on Type.
My Compiler show this Error
Error 13 Impossibile to convert 'System.Linq.Expressions.Expression<System.Func<MLOLPlus.Business.Dealer,string>>' in '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 is a base abstract class base of all Custom Class Data type
Example:
- User inherits IdentityEntity
- Editor too
Base Class 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;
}
}
Custom User Multisort
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;
}
}
Custom Editor 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;
}
}