Skip to content

Using Invoke via a method group produces a different expanded expression to an explicit call to Invoke, (x => func.Invoke(x)) #229

Description

@simonmckenzie

Hi,

Thanks for the great tool!

I've found that if I'm applying an expression via Invoke then Expand, while c# syntax will allow me to omit the parameter for a lambda where the method group's natural type matches that of the expected lambda, e.g. x => func.Invoke(x) can just be replaced with func.Invoke, LinqKit does not produce the same expanded expression for each.

For example:

Expression<Func<int, bool>> isExpensive = p => p > 1000;

Expression<Func<Purchase, bool>> hasAnyExpensivePrices = p => p.Prices.Any(pr => isExpensive.Invoke(pr));
Console.WriteLine(hasAnyExpensivePrices.Expand().ToString());
// Output: p => p.Prices.Any(pr => (pr > 1000))

Expression<Func<Purchase, bool>> hasAnyExpensivePricesMethodGroup = p => p.Prices.Any(isExpensive.Invoke);
Console.WriteLine(hasAnyExpensivePricesMethodGroup.Expand().ToString());
// Output: p.Prices.Any(Convert(Boolean Invoke[Int32,Boolean](System.Linq.Expressions.Expression`1[System.Func`2[System.Int32,System.Boolean]], Int32).CreateDelegate(System.Func`2[System.Int32,System.Boolean], p => (p > 1000)), Func`2))

record Purchase(int[] Prices);

Is this something that can be fixed? It's an easy mistake to make, particularly when ReSharper always suggests I do it!

Image

Activity

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

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