有没有人可以帮助我这个例外? [英] could anyone give me a help in this exception ?

查看:70
本文介绍了有没有人可以帮助我这个例外?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在一个项目上工作,我使用devexpress mvc gridview&我试图分组行

这里是我的代码



hi everyone,
iam working on a project where iam using devexpress mvc gridview & iam trying to group rows
here's my code

public static object Sum(this IQueryable query, string fieldName)
        {
            if (query.Count() == 0)
                return 0;

            var parameter = Expression.Parameter(query.ElementType, string.Empty);
            var propertyInfo = query.ElementType.GetProperty(fieldName);
            var propertyAccess = Expression.MakeMemberAccess(parameter, propertyInfo);
            var propertyAccessExpression = Expression.Lambda(propertyAccess, parameter);
            var expression = Expression.Call(
                typeof(Queryable),
                "Sum",
                new Type[] { query.ElementType },
                query.Expression,
                Expression.Quote(propertyAccessExpression)
            );
            return query.Provider.Execute(expression);
        }







但这里出现错误:






but here the error i got :

No generic method 'Sum' on type 'System.Linq.Queryable' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic.





PLZ任何人都有相同的情况并且得到解决方案会告诉我有什么问题



提前感谢



plz anyone have the same situation &got a solution would tell me what's the wrong

thanks in advance

推荐答案

fieldName 不代表具有支持类型之一的属性:

  • int / Nullable< int>
  • long / Nullable< long> ;
  • float / Nullable< float>
  • double / Nullable< double>
  • decimal / Nullable< decimal>
The fieldName you've passed in doesn't represent a property with one of the supported types:
  • int / Nullable<int>
  • long / Nullable<long>
  • float / Nullable<float>
  • double / Nullable<double>
  • decimal / Nullable<decimal>
var parameter = Expression.Parameter(query.ElementType, string.Empty);
var propertyInfo = query.ElementType.GetProperty(fieldName);
var propertyAccess = Expression.MakeMemberAccess(parameter, propertyInfo);

Expression body;
Type propertyType = propertyAccess.Type;
Type baseType = Nullable.GetUnderlyingType(propertyType); // Returns null if the type is not Nullable<T>

switch (Type.GetTypeCode(baseType ?? propertyType))
{
    case TypeCode.Int32:
    case TypeCode.Int64:
    case TypeCode.Single:
    case TypeCode.Double:
    case TypeCode.Decimal:
    {
        // Supported type - use the property directly:
        body = propertyAccess;
        break;
    }
    case TypeCode.Byte:
    case TypeCode.SByte:
    case TypeCode.Int16:
    case TypeCode.UInt16:
    {
        // Small enough to fit in an Int32:
        Type targetType = typeof(int);
        if (baseType != null) targetType = typeof(Nullable<>).MakeGenericType(targetType);
        body = Expression.Convert(propertyAccess, targetType);
        break;
    }
    case TypeCode.UInt32:
    {
        // Convert to an Int64:
        Type targetType = typeof(long);
        if (baseType != null) targetType = typeof(Nullable<>).MakeGenericType(targetType);
        body = Expression.Convert(propertyAccess, targetType);
        break;
    }
    default:
    {
        // Unknown type - try converting to Double.
        // If there is no conversion, this will throw an exception.
        
        Type targetType = typeof(double);
        if (baseType != null) targetType = typeof(Nullable<>).MakeGenericType(targetType);
        body = Expression.Convert(propertyAccess, targetType);
        break;
    }
}

var propertyAccessExpression = Expression.Lambda(body, parameter);

var expression = Expression.Call(
    typeof(Queryable),
    "Sum",
    new Type[] { query.ElementType },
    query.Expression,
    Expression.Quote(propertyAccessExpression)
);

return query.Provider.Execute(expression);



此外,还有没有内置的 Count 非泛型 IQueryable 界面的扩展方法。除非您已创建自己的版本或导入第三方版本,否则您方法的第一行将无效。


Also, there is no built-in Count extension method for the non-generic IQueryable interface. Unless you've created your own, or imported a third-party version, the first line of your method won't work.


这篇关于有没有人可以帮助我这个例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆