-
Notifications
You must be signed in to change notification settings - Fork 31
Add ExpandEnumMethods to expand enum extension calls into ternary expressions #150
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
base: master
Are you sure you want to change the base?
Conversation
…enum method expansion Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
|
@copilot I see that methods interpretation is not straightforward at all during source generation, so I want to try something, using the ProjectableEnumMethodAttribute which tends to describe extension methods for what they return, it can be used without argument if the method returns the enum member attribute of type T, one argument (the attribute type) if it returns the attribute of this type from the enum member, or two args : attribute type and property name : if the method returns the attribute's property value. |
…ation Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Implemented in becae46. Added
The generator now requires this attribute on any method called with Added functional tests that verify the SQL generation for filtering, selecting, and ordering by expanded enum properties. |
|
@copilot I was wrong to want to in-line attribute values in the expression tree. All we need is to call the enum methods on the enum members, in the expression tree and let EF do the rest. |
…ning values Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Implemented in f42ef49. Removed Example generated expression: @this.Status == Status.Pending ? EnumExtensions.GetDisplayName(Status.Pending) : @this.Status == Status.Approved ? EnumExtensions.GetDisplayName(Status.Approved) : nullAdded a test for enum members on navigation properties as requested. |
|
@copilot Add new tests (unit et integration) that validate that other types are supported for enum expansion : boolean, integer, etc, and add new test showing that enum extension methods with parameters work well, fix if it does not work. |
…rs, update README Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Implemented in e7b388e. Added:
|
Adds a new
ExpandEnumMethodsproperty to theProjectableAttributethat expands enum extension method calls into a chain of ternary expressions for each enum value. This enables EF Core to translate these expressions to SQL CASE statements, making enum methods usable inWhere()andOrderBy()clauses.How it works
When
ExpandEnumMethods = trueis set on a projectable property, enum method calls are expanded into ternary expressions that call the method on each enum member. EF Core then evaluates these method calls at runtime.Supported return types
nullas fallbackdefault(bool)(false) as fallbackdefault(int)(0) as fallbackdefault(T)as fallbackFeatures
Example
SQL Generated
EF Core translates the expanded expressions to CASE statements:
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.