Skip to content

Conversation

Copy link

Copilot AI commented Jan 21, 2026

Adds a new ExpandEnumMethods property to the ProjectableAttribute that 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 in Where() and OrderBy() clauses.

How it works

When ExpandEnumMethods = true is 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.

@this.Status == Status.Pending ? EnumExtensions.GetDisplayName(Status.Pending) : @this.Status == Status.Approved ? EnumExtensions.GetDisplayName(Status.Approved) : null

Supported return types

  • String - returns null as fallback
  • Boolean - returns default(bool) (false) as fallback
  • Integer - returns default(int) (0) as fallback
  • Other value types - returns default(T) as fallback

Features

  • Works with any enum extension method without special configuration
  • Supports nullable enum types (wraps expansion in null check)
  • Supports methods with parameters
  • Supports enum properties on navigation properties

Example

public enum OrderStatus
{
    [Display(Name = "Pending Review")]
    Pending,
    [Display(Name = "Approved")]
    Approved,
}

public static class EnumExtensions
{
    public static string GetDisplayName(this OrderStatus value) { ... }
    public static bool IsApproved(this OrderStatus value) { ... }
    public static string Format(this OrderStatus value, string prefix) { ... }
}

public class Order
{
    public OrderStatus Status { get; set; }
    
    [Projectable(ExpandEnumMethods = true)]
    public string StatusName => Status.GetDisplayName();
    
    [Projectable(ExpandEnumMethods = true)]
    public bool IsStatusApproved => Status.IsApproved();
    
    [Projectable(ExpandEnumMethods = true)]
    public string FormattedStatus => Status.Format("Status: ");
}

SQL Generated

EF Core translates the expanded expressions to CASE statements:

SELECT CASE
    WHEN [o].[Status] = 0 THEN N'Pending Review'
    WHEN [o].[Status] = 1 THEN N'Approved'
END AS [StatusName]
FROM [Orders] AS [o]
Original prompt

This section details on the original issue you should resolve

<issue_title>Expand enum types properties' extension methods to the generated expression tree</issue_title>
<issue_description>While it is not useful in .Select() projections because it is resolved on runtime by EF core, it is useful when used in .Where() and .OrderBy()

Given this example :

public static class EnumExtensions
{
    public static T? GetAttribute<T>(this Enum value) where T : Attribute
    {
        var type = value.GetType();
        var memberInfo = type.GetMember(value.ToString()).FirstOrDefault();
        return memberInfo?.GetCustomAttribute<T>();
    }
}

namespace Foo {
    public static class EntityExtensions
    {
        public enum CustomEnum
        {
            [Display(Name = "Value 1")]
            Value1,
            
            [Display(Name = "Value 2")]
            Value2,
        }
        
        public record Entity
        {
            public int Id { get; set; }
            public CustomEnum? MyValue { get; set; }
            
            [Projectable(EnumPropertiesExapend = true)] // New ProjectableAttribute parameter here
            public string? MyEnumName => MyValue?.GetAttribute<DisplayAttribute>()?.Name;
        }
    }
}

MyEnumName would be expanded to an sequence of ternary expressions (because switch are not suppored in expression trees), like this :

public string MyEnumName => MyValue == CustomEnum.Value1 ? "Value 1" : MyValue == CustomEnum.Value2 ? "Value 2" : null;

Here, GetAttribute() is just an example, it could be any user specified extension, the value would be resolved during compilation to the expression tree source generation.
</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 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.

…enum method expansion

Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Copilot AI changed the title [WIP] Expand enum types properties' extension methods Add ExpandEnumMethods to expand enum extension calls into ternary expressions Jan 21, 2026
Copilot AI requested a review from PhenX January 21, 2026 20:32
@PhenX
Copy link
Collaborator

PhenX commented Jan 21, 2026

@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.
Please change the evaluation code to use it, and throw a compilation error if it is not present.
Also, add functional tests

Copilot AI and others added 2 commits January 21, 2026 21:15
…ation

Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Copy link
Author

Copilot AI commented Jan 21, 2026

@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 extensio...

Implemented in becae46.

Added ProjectableEnumMethodAttribute which describes what an enum extension method returns:

  • [ProjectableEnumMethod(typeof(DisplayAttribute), nameof(DisplayAttribute.Name))] - returns the attribute's property value
  • [ProjectableEnumMethod(typeof(DescriptionAttribute))] - returns the attribute's constructor value

The generator now requires this attribute on any method called with ExpandEnumMethods = true and reports error EFP0003 if missing.

Added functional tests that verify the SQL generation for filtering, selecting, and ordering by expanded enum properties.

@PhenX
Copy link
Collaborator

PhenX commented Jan 22, 2026

@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.
Please remove ProjectableEnumMethodAttribute, it's uses and the method evaluation class because it is not needed as the source generator won't need it.
Adapt the unit tests, ans also add tests with enum members on navigation properties.

…ning values

Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Copy link
Author

Copilot AI commented Jan 22, 2026

@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.
Please rem...

Implemented in f42ef49.

Removed ProjectableEnumMethodAttribute, EnumMethodCallEvaluator, and the EFP0003 diagnostic. The generator now calls enum methods directly on enum members in the expression tree, letting EF Core evaluate them at runtime.

Example generated expression:

@this.Status == Status.Pending ? EnumExtensions.GetDisplayName(Status.Pending) : @this.Status == Status.Approved ? EnumExtensions.GetDisplayName(Status.Approved) : null

Added a test for enum members on navigation properties as requested.

@PhenX
Copy link
Collaborator

PhenX commented Jan 22, 2026

@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.
Update the readme according to the changes in the pull request, for the end user.

Copilot AI and others added 2 commits January 22, 2026 16:59
…rs, update README

Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Copy link
Author

Copilot AI commented Jan 22, 2026

@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 paramete...

Implemented in e7b388e.

Added:

  • Unit tests for boolean (ExpandEnumMethodsReturningBoolean), integer (ExpandEnumMethodsReturningInteger), and methods with parameters (ExpandEnumMethodsWithParameter, ExpandEnumMethodsWithMultipleParameters)
  • Functional tests for filtering/selecting boolean enums (FilterOnBooleanEnumExpansion, SelectBooleanEnumExpansion), ordering by integer (OrderByIntegerEnumExpansion), and methods with parameters (SelectEnumMethodWithParameter)
  • Fixed generator to use default(T) for non-nullable value types instead of null
  • Updated README with comprehensive documentation for the ExpandEnumMethods feature, including examples for string, boolean, integer return types and methods with parameters

@PhenX PhenX marked this pull request as ready for review January 22, 2026 17:22
@PhenX PhenX requested a review from koenbeuk January 24, 2026 13:06
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

Successfully merging this pull request may close these issues.

Expand enum types properties' extension methods to the generated expression tree

3 participants