这个问题在此指的是我刚才的问题在这里 通过@xanatos建议和()的表达完美。我还尝试了针对 double 类型字段的 Max() 表达式,但没有遇到任何问题。作为下一步,我决定添加 DateTime 字段并为其计算 Max()。我修改了 GroupSum 类如下:private class GroupSum : GroupKey{ public Double AggN0 { get; set; } public DateTime AggD0 { get; set; }}并编写了函数:private static Func<IGrouping<GroupKey, DataRow>, DateTime> GetFuncMaxDateTime(){ MethodInfo methInfo = typeof(DataRowExtensions).GetMethod("Field", new Type[] { typeof(DataRow), typeof(string) }); ParameterExpression expRow = Expression.Parameter(typeof(DataRow), "row"); //Parametr: (row =>....) PropertyInfo propertyInfo = typeof(GroupSum).GetProperty("AggD0"); MethodCallExpression expCall = GetFieldCallExpression(expRow, methInfo, propertyInfo.PropertyType, "DocumentDate"); var expRowValues = Expression.Lambda(expCall, expRow); ParameterExpression expQuerygroup = Expression.Parameter(typeof(IGrouping<GroupKey, DataRow>), "g"); //HERE it throws an error: No generic method 'Max' on type 'System.Linq.Enumerable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic. MethodCallExpression expMaxRows = Expression.Call(typeof(Enumerable), "Max", new[] { expRow.Type }, expQuerygroup, expRowValues); var max = Expression.Lambda<Func<IGrouping<GroupKey, DataRow>, DateTime>>(expMaxRows, expQuerygroup); return max.Compile();}代码可以编译,但在运行时会引发错误。我评论了指令并指定了错误消息。这很奇怪,因为唯一的区别是double Sum(double)vsDateTime Max(DateTime)无需添加硬编码版本可以正常工作。private static IEnumerable<GroupSum> GetListOfGroupedRows(IEnumerable<IGrouping<GroupKey, DataRow>> queryGroup){ IEnumerable<GroupSum> querySelect = queryGroup .Select(g => new GroupSum { KeyS0 = g.Key.KeyS0, KeyS1 = g.Key.KeyS1, AggN0 = g.Sum(row => row.Field<double>("Amount")), AggD0 = g.Max(row => row.Field<DateTime>("DocumentNumber")) }); return querySelect;}
1 回答
- 1 回答
- 0 关注
- 286 浏览
添加回答
举报
0/150
提交
取消