本文介绍了不能使用类型'System.DateTime的'表达的返回类型'System.Object的“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了我使用的排序工作正常的表现,直到我打了一个的DateTime 字段,在那里我得到以下错误(在第二线):

I've created an expression that I'm using for sorting which works fine, until I hit a DateTime field, where I get the following error (on the second line):

不能用于返回类型
'System.Object的类型的System.DateTime'的表达

下面是我的代码:

ParameterExpression param = Expression.Parameter(typeof(MyEntity), "x");

Expression<Func<MyEntity, object>> sortExpression =
    Expression.Lambda<Func<AMyEntity, object>>(
        Expression.Property(param, sortKey), param);



谁能帮助呢?

Can anyone help at all?

推荐答案

就在那里添加一个转换:

Just add a conversion in there:

Expression<Func<MyEntity, object>> sortExpression =
    Expression.Lambda<Func<AMyEntity, object>>(
        Expression.Convert(
            Expression.Property(param, sortKey),
            typeof(object)),
        param);

这篇关于不能使用类型'System.DateTime的'表达的返回类型'System.Object的“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 23:53