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

Enum Support #33

Closed
OculiViridi opened this issue Jun 12, 2023 · 3 comments · Fixed by #37
Closed

Enum Support #33

OculiViridi opened this issue Jun 12, 2023 · 3 comments · Fixed by #37
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@OculiViridi
Copy link

I've a service that builds a dynamic query WHERE condition based on some filters.
I'm trying to use your library to build one of those filter, related to an array of enum values, but I'm getting this Exception at runtime:

The type MyProject.Api.Data.Models.OperationType must have at least one public property.

The user can select one or more of those values and so the WHERE condition has to be built using OR.

The MyProject.Api.Data.Models.OperationType is an enum type defined as follows:

public enum OperationType : byte
{
    DT = 1,
    NP = 2,
    Tx = 3,
    RE = 4,
    I = 5,
    AT = 6,
    M = 7,
    TR = 8,
    TxAT = 9
}

My actual code for building the query is this:

...
if (filter.OperationTypes?.Any() == true)
{
    // Conversion from API enum model to EF enum model
    var operationTypes = filter.OperationTypes.Select(c => Enum.Parse<Data.Models.OperationType>(c.ToString(), true)).ToArray();
	
    // Call to AsQueryableValues that causes the Exception
    var queryableValues = _dbContext.AsQueryableValues(operationTypes);

    query = query.Where(x => x.Movements.OperationType != null && queryableValues.Contains(x.Movements.OperationType.Value));
}
...

Am I doing something wrong? How am I supposed to use the library with enums?

@yv989c
Copy link
Owner

yv989c commented Jun 12, 2023

Hi @OculiViridi , unfortunately the library doesn't support this use case at the moment; it supports the following simple types or complex types.

Having said that, what if you project your enum as its underlying data type –assuming byte per your stub– and use that instead? like this:

...
if (filter.OperationTypes?.Any() == true)
{
    // Conversion from API enum model to EF enum model
    // *** Notice the cast to byte ***
    var operationTypes = filter.OperationTypes.Select(c => Enum.Parse<Data.Models.OperationType>(c.ToString(), true)).Select(i => (byte)i);
	
    // Call to AsQueryableValues that causes the Exception
    var queryableValues = _dbContext.AsQueryableValues(operationTypes);

    // *** Notice the cast to byte ***
    query = query.Where(x => x.Movements.OperationType != null && queryableValues.Contains((byte)x.Movements.OperationType.Value));
}
...

If byte doesn't work, try with int.

@OculiViridi
Copy link
Author

If byte doesn't work, try with int.

Seems to work fine both with byte and with int.

@yv989c yv989c changed the title Does it work with enums? Enum Support Jun 14, 2023
@yv989c yv989c added the enhancement New feature or request label Jun 14, 2023
@yv989c yv989c self-assigned this Jun 14, 2023
@yv989c yv989c added this to the v1.0.11 milestone Jun 20, 2023
@yv989c
Copy link
Owner

yv989c commented Jun 22, 2023

Tasks

  • IEnumerable<Enum>
  • Enum property in complex type (IEnumerable<ComplexType>)
  • Update docs

This was referenced Jun 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants