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

LINQKit does not support referencing expressions stored in properties (InvalidCastException) #124

Closed
poizan42 opened this issue Sep 25, 2020 · 6 comments
Labels

Comments

@poizan42
Copy link

poizan42 commented Sep 25, 2020

Running the following program:

using System;
using System.Linq.Expressions;
using LinqKit;

namespace Test
{
    class Program
    {
        public static Expression<Func<int, int>> Foo => x => x * 6;

        static void Main(string[] args)
        {
            Expression<Func<int, int>> bar = x => Foo.Invoke(x * 7);
            Expression<Func<int, int>> barExpanded = bar.Expand(); // <-- System.InvalidCastException
            Console.WriteLine(barExpanded.Invoke(1));
        }
    }
}

Fails with

System.InvalidCastException: Unable to cast object of type 'System.Linq.Expressions.PropertyExpression' to type 'System.Linq.Expressions.LambdaExpression'.
   at LinqKit.ExpressionExpander.VisitMethodCall(MethodCallExpression m) in C:\projects\LINQKit\src\LinqKit.Core\ExpressionExpander.cs:line 78
   at LinqKit.ExpressionVisitor.Visit(Expression exp) in C:\projects\LINQKit\src\LinqKit.Core\ExpressionVisitor.cs:line 71
   at LinqKit.ExpressionVisitor.VisitLambda(LambdaExpression lambda) in C:\projects\LINQKit\src\LinqKit.Core\ExpressionVisitor.cs:line 353
   at LinqKit.ExpressionVisitor.Visit(Expression exp) in C:\projects\LINQKit\src\LinqKit.Core\ExpressionVisitor.cs:line 73
   at LinqKit.Extensions.Expand[TDelegate](Expression`1 expr) in C:\projects\LINQKit\src\LinqKit.Core\Extensions.cs:line 54
   at Test.Program.Main(String[] args) in C:\projects\LINQKit\Test\Program.cs:line 14

This may be related to #89 where it's a delegate rather than an expression in a property that causes problems.

@sdanyliv
Copy link
Contributor

@StefH, I think It should be fixed in #126

@StefH
Copy link
Collaborator

StefH commented Oct 27, 2020

Hello @sdanyliv, thanks for noticing.

If you have time, can you add an extra UnitTest for this scenario? If this is green, then I can close this issue.

@StefH StefH added the bug label Oct 27, 2020
@StefH
Copy link
Collaborator

StefH commented Oct 27, 2020

@poizan42 : Can you please try 1.1.20-preview-01 version in NuGet to see if this fixes your issue?

@poizan42
Copy link
Author

poizan42 commented Nov 9, 2020

It fixes the case I wrote in this issues. But it does not work with deeper nesting, e.g. the test ExpressionCombiner_ExpressionAsProperty_FromExpressionVariable still fails.

@sdanyliv
Copy link
Contributor

Updated PR #127 with fix.

@StefH
Copy link
Collaborator

StefH commented Jan 16, 2021

PR is merged.

@StefH StefH closed this as completed Jan 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants