我有,我希望LINQ表達式,使其成爲通用名稱:C#LINQ表達式作爲參數數據表方法
public class GenericBudgetMatrix
{
public List<string> MatrixHeaders { set; get; }
public List<GenericBudgetMatrixRow> MatrixRow { set; get; }
public GenericBudgetMatrixRow MatrixFooterRow { set; get; }
}
public class GenericBudgetMatrixRow
{
public string EntityName { set; get; }
public List<decimal> Values { set; get; }
public decimal Total { set; get; }
}
GenericBudgetMatrix _genericBudgetMatrix = new GenericBudgetMatrix();
_genericBudgetMatrix.MatrixRow = _matrixTemplate.AsEnumerable().Select(r =>
new GenericBudgetMatrixRow
{
EntityName = r.Field<string>(0),
Values = r.ItemArray.Skip(1).Select(x => Convert.ToDecimal(x)).ToList(),
Total = r.ItemArray.Skip(1).Select(x => Convert.ToDecimal(x)).Sum()
}
).ToList();
我想提出一個新的方法來接受
r =>
new GenericBudgetMatrixRow
{
EntityName = r.Field<string>(0),
Values = r.ItemArray.Skip(1).Select(x =>
Convert.ToDecimal(x)).ToList(),
Total = r.ItemArray.Skip(1).Select(x =>
Convert.ToDecimal(x)).Sum()
}
爲Func表達式參數。
事情是這樣的:
public void GenericMethod(Func<expression> predicate, DataTable _matrixTemplate)
{
_matrixTemplate.AsEnumerable().Select(predicate).ToList();
}
任何想法,可以幫助我建立這個辦法?
您返回'IEnumerable'但調用'ToList()',看起來很奇怪。我建議刪除'ToList()'並返回純IEnumerable或將返回類型改爲'List'或'IList'。 – Dmitry
我試過了,但是語法有問題: – santi
@santi你能更具體些嗎? – HimBromBeere