score:3

Accepted answer

Have you tried ILSpy? It even has some options to control if you want to produce LINQ "function" syntax or from... where syntax (View->Options->Decompiler->Decompile query expression and Decompile anonymous methods/lambdas)

For your other question: SubFunctionEntity.get_Volume is the getter of the property Volume of the class SubFunctionEntity.

(MethodInfo) methodof(SubFunctionEntity.get_Volume) is the same as typeof(SubFunctionEntity).GetProperty("Volume").GetGetMethod(), only done at compile time instead that at runtime. The problem is that C# doesn't have a methodof "function".

If you really want to use what the Reflector gave you, this is the "corrected" version:

public double? LoadSumRialAmount(long functionId, long? subFunctionId)
{
    ParameterExpression expression;
    if (!subFunctionId.HasValue)
    {
        return (from func in base.MetaData.SubFunction
                where func.FunctionId == functionId
                select func).Select<SubFunctionEntity, double?>(Expression.Lambda<Func<SubFunctionEntity, double?>>(Expression.Multiply(Expression.Property(expression = Expression.Parameter(typeof(SubFunctionEntity), "func"), typeof(SubFunctionEntity).GetProperty("Volume").GetGetMethod()), Expression.Property(expression, typeof(SubFunctionEntity).GetProperty("RialAmount").GetGetMethod())), new ParameterExpression[] { expression })).Sum();
    }
    return (from func in base.MetaData.SubFunction
            where (func.FunctionId == functionId) && (func.SubFunctionId != subFunctionId)
            select func).Select<SubFunctionEntity, double?>(Expression.Lambda<Func<SubFunctionEntity, double?>>(Expression.Multiply(Expression.Property(expression = Expression.Parameter(typeof(SubFunctionEntity), "func"), typeof(SubFunctionEntity).GetProperty("Volume").GetGetMethod()), Expression.Property(expression, typeof(SubFunctionEntity).GetProperty("RialAmount").GetGetMethod())), new ParameterExpression[] { expression })).Sum();
}

Note that you'll need some using:

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

Ah... the most complex expression is : func => (func.Volume * func.RialAmount) so you could write the method as:

public double? LoadSumRialAmount(long functionId, long? subFunctionId)
{
    if (!subFunctionId.HasValue)
    {
        return (from func in base.MetaData.SubFunction
                where func.FunctionId == functionId
                select func).Select(func => (func.Volume * func.RialAmount)).Sum();
    }
    return (from func in base.MetaData.SubFunction
            where (func.FunctionId == functionId) && (func.SubFunctionId != subFunctionId)
            select func).Select(func => (func.Volume * func.RialAmount)).Sum();
}

Addendum: checked, the ILSpy produce random garbage similar-but different to Reflector


Related Articles