LINQ Expressions Body ToString

dapper lambda linq regex string

Question

I'm wondering if somebody can explain to me the downside of retrieving

(Expression<Func<T, bool>>) Expression.Body

at runtime and operating on this as a string?

for example given the following

public partial class Tests : Form
{
    public Tests()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        TestClass t = new TestClass();
        textBox1.Text = t.Evaluate(templates => templates.usable == true && templates.name == "tc1");
    }       
}

public class TestClass
{
    public string name { get; set; }
    public bool usable { get; set; }
}

public static class Helpers
{
    public static string Evaluate<T>(this T input,Expression<Func<T, bool>> Expression) where T : class
    {

        return Expression.Parameters.Single().Name + " " + Expression.Body;

    }
}

which returns

templates ((templates.usable == True) AndAlso (templates.name == "tc1"))

I'm wondering what sort of performance issues may arise from retrieving this and then parsing it through a Regex expression

in answer to the question why? I've been playing around with Dapper a bit, but none of the pre existing extensions (that I've seen) appeal to me

I would like to be able to operate on it in a similar fashion to EF for example

_repo.Select(templates=>templates.usable== true && templates.id == templateId);

Popular Answer

Thanks for the nudge towards using the actual expression rather than parsing the text. for anybody that's interested I've posted a link to the first draft of my sqlexpression project.Google Code - SqlExpression.cs

The parameters that are created are DynamicParameters from Dapper but could easily be swapped for a standard sql parameter

haven't done alot of testing yet but query appears to perform reasonably well at this stage




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why