Skip to content

Unable to resolve UseMemberBody with method #134

@gavinltomra

Description

@gavinltomra

There doesn't seem to be much documentation for UseMemberBody and even less on how to use it with methods.

public class Foo
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public static class FooExtensions
{
    [Projectable]
    public static bool NameEquals(this Foo a, Foo b) => a.Name == b.Name;
}

// ...
var example = new Foo { Name = "test" };
var result = db.Foos.Where(f => f.NameEquals(example)).ToList();

The above code compiles and seems to work. But when I try to use another member as the body, it can't figure it out:

public static class FooExtensions
{
    [Projectable(UseMemberBody = nameof(NameEqualsExpr))]
    public static bool NameEquals(this Foo a, Foo b) => a.Name == b.Name;

    private static Expression<Func<Foo, Foo, bool>> NameEqualsExpr => (a, b) => a.Name == b.Name;
}

Results in Unable to resolve generated expression for TestApp.FooExtensions.NameEquals.

It looks like the problem might be that the UseMemberBody handling doesn't process static methods correctly? It looks like it's expecting an additional parameter of the FooExtensions class type, which of course makes no sense for a static method.

This doesn't appear to work for instance methods either:

public class Foo
{
    // ...
    [Projectable(UseMemberBody = nameof(NameEqualsExpr))]
    public bool NameEquals(Foo b) => Name == b.Name;

    private static Expression<Func<Foo, Foo, bool>> NameEqualsExpr => (a, b) => a.Name == b.Name;
}

The check in GetExpressionFromMemberBody seems suspicious in that it appears to be expecting the this parameter to be last (which is not how that works).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions