0

I want to convert Expression<Func<Loan, bool>> To string and vice versa. can I do it? how can implement ConvertStringToExpression method?

internal class Program
{
    public class Loan
    {
        public bool IsActive { get; set; }
    }

    private static void Main(string[] args)
    {
        Expression<Func<Loan, bool>> expression = l => !l.IsActive;

        var expStr = ConvertExpressionToString(expression);

        var exp = ConvertStringToExpression(expStr);
    }

    public static string ConvertExpressionToString(Expression<Func<Loan, bool>> expression)
    {
        //return ???
        throw new NotImplementedException();
    }

    public static string ConvertStringToExpression(string expression)
    {
        //return ???
        throw new NotImplementedException();
    }
}
ArMaN
  • 2,306
  • 4
  • 33
  • 55
  • 1
    How would you expect this to work?? And even more importantly, why on earth would you want to do this? – David L Feb 19 '16 at 16:18
  • 1
    In short: You will have to parse the string using `Microsoft.CodeAnalysis.CSharp.SyntaxFactory`, get a (most likely) `Microsoft.CodeAnalysis.CSharp.Syntax.SimpleLambdaExpressionSyntax`, get the expression based on what you put in it and end up with a `System.Linq.Expressions.LambdaExpression`. – Mark Feb 19 '16 at 16:24
  • @DavidL I want to save expressions as string (not xml) in database. – ArMaN Feb 19 '16 at 16:25
  • 1
    As far as I know, you can't just *convert* a string to an expression because there are multiple types of expressions you might want as an output ([ConstantExpression](https://msdn.microsoft.com/en-us/library/system.linq.expressions.constantexpression(v=vs.110).aspx), [MemberExpression](https://msdn.microsoft.com/en-us/library/system.linq.expressions.memberexpression(v=vs.110).aspx), [MethodCallExpression](https://msdn.microsoft.com/en-us/library/system.linq.expressions.methodcallexpression(v=vs.110).aspx)) – Erik Philips Feb 19 '16 at 16:26
  • @Mark can you help me by an example? – ArMaN Feb 19 '16 at 16:29
  • @ArMaN The custom one we're using at work is several hundreds of lines of code and while you probably won't need all of it I don't have the time at the moment to condense it down to the basics :( So I just posted the basics so you can narrow your search. I'm sure somebody has written a library or blog article that does stuff like this. – Mark Feb 19 '16 at 16:48
  • @Mark ok, what is your solution to convert expression to string? when I use toString method, the result has not expression format. it added AndAlso or OrElse or Not... – ArMaN Feb 19 '16 at 20:29

1 Answers1

0

You can use DynamicLinq library as detailed in ScottGu's blog a long time ago. This library have a class ExpressionParser which has all the component of parsing framework to parse string expressions and convert them back to Expression.

Here's one of the usage:

    public static Expression Parse(Type resultType, string expression, params object[] values)
    {
        ExpressionParser parser = new ExpressionParser(null, expression, values);
        return parser.Parse(resultType);
    }

I have created a gist for the single file library here. But you can also download the complete content here.

vendettamit
  • 14,315
  • 2
  • 32
  • 54
  • This doesn't solve the OP's issue, since it doesn't take JUST a string. He wants to be able to store a string and recreate an expression based off that string. – David L Feb 19 '16 at 18:06
  • It's not that simple as OP thinking..how the Expression type would know the type of Lambda. It might be possible but that would be done via reflection and that means it will be all the way reflection from there. Also this library is quite old and might not be able to generate full lambda. But the library would be good place to start. – vendettamit Feb 19 '16 at 18:15
  • I'm not disagreeing whatsover :). This absolutely is more complicated than the OP's expectations. That said, this still doesn't solve it for the OP. – David L Feb 19 '16 at 18:17