我試着用一個基本的方法來做出一個基類,該方法用一個依賴於類型的函數來排序列表。泛型C#依賴於類型的方法組織
我的編譯器顯示此錯誤
錯誤13 Impossibile轉換 'System.Linq.Expressions.Expression < System.Func < MLOLPlus.Business.Dealer,串> >' 在「System.Linq.Expressions.Expression < System.Func < T,字符串> >'。 d:\ Documenti \ Lavori \的TImage \ MLOLPlus \ SRC \ MLOLPlus.Business \ DataAcess \ DataTablesClasses \ DataTableMultiSort.cs 197個20 MLOLPlus.Business
IdentityEntity是所有自定義類數據類型
的基抽象類基例如:
- 用戶繼承IdentityEntity
- 編輯器太
基類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;
}
}
自定義用戶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;
}
}
自定義編輯器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;
}
}
是的,我的錯誤,但看完後的代碼不是很重要的編譯器錯誤,我假設這是我的課程中缺乏組織的機構 – andrea 2014-11-05 14:25:48
我不明白意大利語,但編譯器抱怨您沒有包括的課程:RetailerMultiSort – ShayD 2014-11-05 14:25:54
請將您的代碼縮減爲[儘可能最短的完整可編譯示例](http://sscce.org /)來關注你的問題。 – 2014-11-05 14:26:18