简体   繁体   中英

Expression.Call, Int32 and Enum

Can't figure out how to build an expression that compares an enum type with an int. I have a MVC site with Kendo components embedded. In the view-class, the property is an ENUM, but the view returns an Int32 (the source is a Kendo IFilterDescriptor).

So the problem is...: I receive an int from the view, build the expression, that fails because an enum is expected. Fix this by converting the int to its enum representation, but then it fails when querying the database, because the database expect an Int32.

    public static Expression<Func<DOMAIN, bool>> GetExpressionsFromFilterDescription<DOMAIN, VIEW>(this IEnumerable<IFilterDescriptor> _this)
    {
        Expression expressions = null;
        ParameterExpression pe = Expression.Parameter(typeof(DOMAIN), "x");

        foreach (FilterDescriptor item in _this)
        {
            MemberExpression member = Expression.Property(pe, item.Member);
            ConstantExpression value = Expression.Constant(item.Value);
            Expression exp = null;

            switch (item.Operator)
            {
                case FilterOperator.IsEqualTo:
                    exp = Expression.Equal(member, value);

From what I understand, Expression.Call() should be able to fix this, I just can't figure out how to do this.

Any help would be appreciated.

BR Peter

UPDATE

Converting the value like below, does fix the expression problem (the "The binary operator Equal is not defined for the types..." error), but then I get a new error querying the database: "Type mismatch in NHibernate.Criterion.SimpleExpression: Status expected type System.Int32, actual type...".

exp = Expression.Equal(member, Expression.Convert(value, typeof(MyEnum)));

The fix, as I see it, is either by building my own compare (Expression.Call ?) or by telling that the type (in item.Member) is an int and not an enum, but I don't know how or if this is the right way. Thanks.

UPDATE UPDATE

It seems like that the second part of the problem is due to NHibernate.QueryOver and its limitations. Once changed to NHibernate.Linq, the query part of the problem, went away.

As for the Expression part I have fixed the problem by adding an attribute to the property telling how the value should be converted. I am not using Expression.Convert (but I could have), the conversion happens in the received filter description before building the expression.

Thanks for your time and help. I will accept the answers related to Expression.Convert since it could fix the problem. I still do want to understand the Expression.Call() method - so please feel free to comment on that. Thanks.

您可以使用Expression.Convert将枚举type to an的表达式转换为int`(假设枚举的基础类型是int)。

Wouldn't this do the trick?

MemberExpression member = Expression.Convert(
                            Expression.Property(pe, item.Member), typeof(int);
ConstantExpression value = Expression.Constant((int)item.Value);

I don't exactly see your enum you are comparing to, is it item.Value?

You can always cast the value of an enum to a number to compare it to a number or feed it into systems that don't recognize enums:

enum Blah
{
    Tom,
    Rick,
    Harry
}

if ((Int32)Blah.Tom == 0)
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM