What are the challenges you face when working across database platforms? Take the survey
Options

v9 Lambda supplied to argument of Expression<...>

slaneyrwslaneyrw Posts: 46
edited January 14, 2016 5:50PM in .NET Reflector Previous Versions
If I have a method whose parameters is an Expression<Func<...>> then Reflector is not decompiling the resultant expression tree back into the original lambda statement. Are expression decompiling not supported ?

Example. I have code that invokes FirstOrDefaultAsync on an Entity Framework IDbSet<> using the following extension method

EntityFramework v6, System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync

public static Task<TSource> FirstOrDefaultAsync<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)


Original Source
        public override async Task&lt;ApplicationUser&gt; FindAsync(UserLoginInfo login)
        {
            if (login == null)
            {
                throw new ArgumentNullException("login");
            }

            
            var provider = login.LoginProvider;
            var key = login.ProviderKey;
            var userLogin = await this.UserLogins.FirstOrDefaultAsync(l =&gt; l.LoginProvider == provider && l.ProviderKey == key && l.CompanyCode == this.companyContext.CompanyCode).WithCurrentCulture();
            if (userLogin != null)
            {
                var userId = userLogin.UserId;
                return await GetUserAggregateAsync(u =&gt; u.Id.Equals(userId)).WithCurrentCulture();
            }
            return null;
        }

Decompiled
public async override Task&lt;ApplicationUser&gt; FindAsync(UserLoginInfo login)
{
    ParameterExpression expression;
    &lt;&gt;c__DisplayClass8_0 asyncVariable0;
    if (login == null)
    {
        throw new ArgumentNullException("login");
    }
    string provider = login.LoginProvider;
    string key = login.ProviderKey;
    ParameterExpression&#91;&#93; expressionArray1 = new ParameterExpression&#91;&#93; { expression };
    IdentityUserLoginWithCompanyCode asyncVariable1 = await this.UserLogins.FirstOrDefaultAsync&lt;IdentityUserLoginWithCompanyCode&gt;(Expression.Lambda&lt;Func&lt;IdentityUserLoginWithCompanyCode, bool&gt;&gt;(Expression.AndAlso(Expression.AndAlso(Expression.Equal(Expression.Property(expression = Expression.Parameter(typeof(IdentityUserLoginWithCompanyCode), "l"), (MethodInfo) methodof(IdentityUserLogin&lt;string&gt;.get_LoginProvider, IdentityUserLogin&lt;string&gt;)), Expression.Field(Expression.Constant(asyncVariable0, typeof(&lt;&gt;c__DisplayClass8_0)), fieldof(&lt;&gt;c__DisplayClass8_0.provider))), Expression.Equal(Expression.Property(expression, (MethodInfo) methodof(IdentityUserLogin&lt;string&gt;.get_ProviderKey, IdentityUserLogin&lt;string&gt;)), Expression.Field(Expression.Constant(asyncVariable0, typeof(&lt;&gt;c__DisplayClass8_0)), fieldof(&lt;&gt;c__DisplayClass8_0.key)))), Expression.Equal(Expression.Property(expression, (MethodInfo) methodof(IdentityUserLoginWithCompanyCode.get_CompanyCode)), Expression.Property(Expression.Field(Expression.Constant(this, typeof(ApplicationUserStore)), fieldof(ApplicationUserStore.companyContext)), (MethodInfo) methodof(ICompanyContext.get_CompanyCode)))), expressionArray1)).WithCurrentCulture&lt;IdentityUserLoginWithCompanyCode&gt;();
    IdentityUserLoginWithCompanyCode userLogin = asyncVariable1;
    asyncVariable1 = null;
    if (userLogin &gt; null)
    {
        &lt;&gt;c__DisplayClass8_1 asyncVariable2;
        string userId = userLogin.UserId;
        Expression&#91;&#93; arguments = new Expression&#91;&#93; { Expression.Field(Expression.Constant(asyncVariable2, typeof(&lt;&gt;c__DisplayClass8_1)), fieldof(&lt;&gt;c__DisplayClass8_1.userId)) };
        ParameterExpression&#91;&#93; parameters = new ParameterExpression&#91;&#93; { expression };
        return await this.GetUserAggregateAsync(Expression.Lambda&lt;Func&lt;ApplicationUser, bool&gt;&gt;(Expression.Call(Expression.Property(expression = Expression.Parameter(typeof(ApplicationUser), "u"), (MethodInfo) methodof(IdentityUser&lt;string, IdentityUserLoginWithCompanyCode, IdentityUserRole, IdentityUserClaimWithIssuer&gt;.get_Id, IdentityUser&lt;string, IdentityUserLoginWithCompanyCode, IdentityUserRole, IdentityUserClaimWithIssuer&gt;)), (MethodInfo) methodof(string.Equals), arguments), parameters)).WithCurrentCulture&lt;ApplicationUser&gt;();
    }
    return null;
}


If you look at the decompiled code you will also see a strange null check
if (userLogin &gt; null)
Sign In or Register to comment.