mrxten / QueryDesigner

QueryDesigner provides way of creating complex IQueryable filtering based on dynamic expression trees.
MIT License
74 stars 26 forks source link

"Cannot convert value to type Nullable`1. #4

Closed captainDeezN closed 6 years ago

captainDeezN commented 6 years ago

Getting the above error when trying to filter using a property in my model class that is of type int.

Below is my filter declaration:

var filter = new FilterContainer
      {
        Where = new TreeFilter
        {
          OperatorType = TreeFilterType.Or,
          Operands = new List<TreeFilter>
                    {
                        new TreeFilter
                        {
                            Field = "Description",
                            FilterType = WhereFilterType.Contains,
                            Value = "test"
                        },
                          new TreeFilter
                        {
                            Field = "QAApproverId",
                            FilterType = WhereFilterType.Equal,
                            Value = 2

                        },

                    }
        },

        OrderBy = new List<OrderFilter>
                {
                    new OrderFilter
                    {
                        Field = "Id",
                        Order = OrderFilterType.Desc
                    },
                }
      };

And this is a piece of my Model Class:

public int? QAApproverId { get; set; } [ForeignKey("QAApproverId")] public User QAApprover { get; set; }

mrxten commented 6 years ago

Hi, will fix it.

BramscoChill commented 6 years ago

was having this same issue, I am using the entity framework in combination with QueryDesigner. When there are properties of the nullable type in the model, its crashing when filtering with: "System.InvalidCastException: 'Cannot convert value to type Nullable`1.'" inside: WhereExpression.cs --> GenerateExpressionOneField() on: Expression.Equal(). Inside Expression.Equal(), both expressions need to be of the same type. I managed to fix this with the following (in: WhereExpression.cs):

WhereExpression_FIX.txt

mrxten commented 6 years ago

@BramscoChill wow, sorry for late answers, too busy :( Thanks for contribution, am will check this and will add to library :)

mrxten commented 6 years ago

Fixed in 2.1.0-pre