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

Filter by enum column with parameters throws exception #258

Closed
MaklaCof opened this issue Mar 21, 2019 · 10 comments
Closed

Filter by enum column with parameters throws exception #258

MaklaCof opened this issue Mar 21, 2019 · 10 comments
Assignees
Labels

Comments

@MaklaCof
Copy link

MaklaCof commented Mar 21, 2019

I am using version:

<PackageReference Include="System.Linq.Dynamic.Core" Version="1.0.11" />

I am trying to filter by enum and pass value as parameter, but I get exception

Expression of type 'System.Nullable`1[MyModule]' expected

This works:

this.Query<DbMenu>().Where("Module=4")

and this doesn't:

this.Query<DbMenu>().Where("Module=@0", 4)

Entity source:

public enum MyModule { Core, ... }

public class DbMenu
{
    public MyModule? Module {get;set;}
    ...
}

Callstack:

   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseConstantExpressionToEnum(Int32 pos, Type leftType, ConstantExpression constantExpr) in /Parser/ExpressionParser.cs:line 569
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseComparisonOperator() in /Parser/ExpressionParser.cs:line 460
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseLogicalAndOrOperator() in /Parser/ExpressionParser.cs:line 360
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseIn() in /Parser/ExpressionParser.cs:line 280
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseAndOperator() in /Parser/ExpressionParser.cs:line 263
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseOrOperator() in /Parser/ExpressionParser.cs:line 248
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseLambdaOperator() in /Parser/ExpressionParser.cs:line 231
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseNullCoalescingOperator() in /Parser/ExpressionParser.cs:line 218
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.ParseConditionalOperator() in /Parser/ExpressionParser.cs:line 201
   at System.Linq.Dynamic.Core.Parser.ExpressionParser.Parse(Type resultType, Boolean createParameterCtor) in /Parser/ExpressionParser.cs:line 140
   at System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda(ParsingConfig parsingConfig, Boolean createParameterCtor, ParameterExpression[] parameters, Type resultType, String expression, Object[] values) in /DynamicExpressionParser.cs:line 67
   at System.Linq.Dynamic.Core.DynamicQueryableExtensions.Where(IQueryable source, ParsingConfig config, String predicate, Object[] args) in /DynamicQueryableExtensions.cs:line 2011
   at System.Linq.Dynamic.Core.DynamicQueryableExtensions.Where[TSource](IQueryable`1 source, ParsingConfig config, String predicate, Object[] args) in /DynamicQueryableExtensions.cs:line 1983
   at System.Linq.Dynamic.Core.DynamicQueryableExtensions.Where[TSource](IQueryable`1 source, String predicate, Object[] args) in /DynamicQueryableExtensions.cs:line 1989
@StefH
Copy link
Collaborator

StefH commented Mar 23, 2019

What framework do you use?

@MaklaCof
Copy link
Author

Hi @StefH

.NET Core 2.2
Entity Framework 2.2.3
SQL Server

@StefH
Copy link
Collaborator

StefH commented Mar 24, 2019

And does this work when you use a normal linq? Can you provide a full example project?

@MaklaCof
Copy link
Author

Hi @StefH
Enum must be nullable. Here is demo.

@StefH
Copy link
Collaborator

StefH commented Mar 25, 2019

OK. I see.

I will work probably if you use:

this.Query<DbMenu>().Where("Module=@0", (in?) 4)

However, for usability it would be better if this is built-in the code.

@MaklaCof
Copy link
Author

It would be a lot better. Because I don't know if enum is nullable. I am using same code for all tables, so I don't know at compile time if table has any enum and if this enum is nullable. I could do it with reflection, but out of the box would be awesome.

@StefH StefH added the bug label Mar 25, 2019
@StefH StefH self-assigned this Mar 25, 2019
@StefH
Copy link
Collaborator

StefH commented Mar 25, 2019

Problem is found and fixed.

Can you try a preview version:
System.Linq.Dynamic.Core.1.0.12-ci-11129.nupkg

For using MyGet preview versions, see : https://github.com/StefH/System.Linq.Dynamic.Core/wiki/MyGet-preview-versions

@MaklaCof
Copy link
Author

It works. Thank you.

@StefH
Copy link
Collaborator

StefH commented Mar 26, 2019

Official NuGet (1.0.12) is uploaded and will be visible in some time...

@Genotypek
Copy link

Genotypek commented Dec 17, 2020

Hey @StefH.
I found another problem:

DynamicExpressionParser.ParseLambda<TestEnum, bool>(ParsingConfig.DefaultEFCore21, true, "x => x==@0", TestEnum.Enum2);

It doesn't work. It can convert it into Expression, when enum is inside any class as property. But it doesn't work for Enum type itself.

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

No branches or pull requests

3 participants