Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datatype of Filter.Value needs to be string instead of object #21

Open
patelharshilb opened this issue Jan 17, 2016 · 3 comments
Open

Comments

@patelharshilb
Copy link

The datatype of Filter.Value needs to be of type string instead of 'object'. The reason behind this is when we post the data from the kendo-ui grid to asp.net controller's DataSourceRequest parameter, the value field gets populated with a string[1](array of strings with length=1). Thus when executing the query into SQL it throws below error:

Operator '=' incompatible with operand types 'String' and 'String[]'

If the 'Filter.Value' is of type string, it works just as expected.

@kalavt
Copy link

kalavt commented Mar 3, 2016

To resolve ERROR GlobalExceptionHandling.OnException - Operator '!=' incompatible with operand types 'Guid' and 'String' (at index 10)
I have changed the values to typed values through ConvertType
`

private static IQueryable Filter(IQueryable queryable, Filter filter)
{
if (filter != null && filter.Logic != null)
{
// Collect a flat list of all filters
var filters = filter.All();

            // Get all filter values as array (needed by the Where method of Dynamic Linq)
            var values = filters.Select(f => ConvertType<T>(f.Value, f.Field)).ToArray();

            // Create a predicate expression e.g. Field1 = @0 And Field2 > @1
            string predicate = filter.ToExpression(filters);

            // Use the Where method of Dynamic Linq to filter the data
            queryable = queryable.Where(predicate, values);
        }

        return queryable;
    }

    private static object ConvertType<T>(string value, string field)
    {
        var type = typeof(T).GetProperty(field).PropertyType;
        return TypeDescriptor.GetConverter(type).ConvertFromString(value);
    }

`

@jferguson-spitfiregroup
Copy link

Ah perfect thx. That's just what I needed. This should be a Pull Request item. Right now it only works for strings only.

@maulik-modi
Copy link

@kalavt or @jferguson-spitfiregroup , please raise PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants