mrxten / QueryDesigner

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

Can't use WhereFilterType.Contains for property of type int #14

Closed jetro44 closed 5 years ago

jetro44 commented 5 years ago

When I use filter for searching objects by property of type int (or non-string but numeric) with WhereFilterType.Contains exp is thrown.

I tried this:

if (prop.Type == typeof(string))
                    {
                        return Expression.Call(prop, ContainsMethod, Expression.Constant(filter.Value, StringType));
                    }

                    if (int.TryParse(filter.Value.ToString(), out int numericValue))
                    {
                        return Expression.Call(prop, ContainsMethodAsInt, Expression.Constant(numericValue, IntType));
                    }

but I got error:

The binary operator OrElse is not defined for the types 'System.Boolean' and 'System.Int32'. System.Linq.Expressions.Expression.OrElse(Expression left, Expression right, MethodInfo method) System.Linq.Expressions.Expression.OrElse(Expression left, Expression right)

Any idea how to search by numeric property?

mrxten commented 5 years ago

Contains method searching only in string.

jetro44 commented 5 years ago

@mrxten is there any way, how to search in numeric type with usage of QueryDesigner?

mrxten commented 5 years ago

No ofc. How you imagine it in Expression and finally in SQL?

On 4 Mar 2019, at 11:09, jetro44 notifications@github.com wrote:

@mrxten is there any way, how to search in numeric type with usage of QueryDesigner?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

jetro44 commented 5 years ago

@mrxten

I'm looking for something like this:

SELECT Id,ColumnAsInt     
FROM SomeTable
WHERE ColumnAsInt like CAST(527 as nvarchar) + '%'

527 is search value

RESULT:

Id  ColumnAsInt 
3742    52700
3416    52701
2578    52704
2448    52705
mrxten commented 5 years ago

Hmm, could you show how would you implement this request via Entity Framework?

jetro44 commented 5 years ago

@mrxten

I see, why you ask this question... CAST seems to be not supported in EF, but it is possible to call something like this:

query.where(o => EF.Functions.Like(o.ColumnAsInt.ToString(), "%" + searchValue + "%"))

I apologize for my insufficient knowledge. However, this is one of the reasons I would like to implement searching via your QueryDesigner. If you have any idea, how to search in int columns (and other numerous types), I will appreciate it.

Nevertheless, thanks for QD, its helpful.

mrxten commented 5 years ago

Entity framework doesn't support extended method ToString. I'm afraid this cannot be implement via .net expression. Or I just didn't find way.

jetro44 commented 5 years ago

Thanks @mrxten. I'll try to make some overriding on business layer of our solution (but in combination of your QD). We should wait for EF improvement...