none
Expression delegate for dummies RRS feed

  • Question

  • Dear all,

    I have seen some sample code in asp.net core where they make the difference betwwen delegate and Expression delegate as sample code below :

    // Lambda expression as executable code.
    Func<int, bool> deleg = i => i < 5;
    // Invoke the delegate and display the output.
    Console.WriteLine("deleg(4) = {0}", deleg(4));
    
    // Lambda expression as data in the form of an expression tree.
    System.Linq.Expressions.Expression<Func<int, bool>> expr = i => i < 5;
    // Compile the expression tree into executable code.
    Func<int, bool> deleg2 = expr.Compile();
    // Invoke the method and print the output.
    Console.WriteLine("deleg2(4) = {0}", deleg2(4));
    
    /*  This code produces the following output:
    
        deleg(4) = True
        deleg2(4) = True
    */

    I do not understand the benefit of using 

    System.Linq.Expressions.Expression

    Could you please try to explain me with simple words what is the main advantage for using this complex reading syntax ?

    Thanks fro feedback

    regards

    Wednesday, July 31, 2019 1:47 PM

Answers

  • Hi wakefun, 

    Thank you for posting here.

    For you question, you want to know more about Expression Tree.

    Actually, Expression Tree is a kind of data structure and has different compilations comparing to delegate.

    Here’s a sample of Expression Tree.

               Expression<Func<int, int, int>> expression = 
                                    (a, b)=> a * b + 2;
    

    Following is the data structure.


    We need Expression because it is much easier to translate a data structure such as an expression tree into SQL than it is to translate raw IL or executable code into SQL.

    Besides, I have found two useful references.

    The first reference explain reasons to use Expression<Func<T>>.

    Why would you use Expression<Func<T>> rather than Func<T>?

    The second reference can help you understand Expression Tree.

    Expression Tree Basics

    Hope them can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by wakefun Wednesday, August 7, 2019 8:13 AM
    Thursday, August 1, 2019 8:44 AM
    Moderator

All replies

  • Hi,

    Although both of your constructs do the same thing, they are very different in how they work.

    A Func<T> is a delegate, and points to a method to execute. This method can be defined as an actual method, an anonymous method, an inline method or a lambda.

    An Expression is a complex data structure that contains the complete tree structure of a C# expression. Compiling an expression, and thus converting it, can result in a Func<T>.

    For normal use, you should not need to use Expressions.

    Expression can be usefull however, if you need more knowledge about the expression, and not just want to execute it. Entity Framework (a framework that does database communication based on Object Oriented objects called Entities) makes heavy use of Expressions to translate C# Lambda Expressions into SQL queries.

    If Entity Framework would use a delegate like data.Where(x=>x.Age > 45), your SQL database cannot execute the delegate immediately. The delegate points to a piece of C# code in memory and is not a SQL query. If we define the lambda expression as an Expression<Func<T,bool>>, without compiling it. Entity Framework can look at the expression tree and translate it to a SQL query without executing it.

    Try to put a breakpoint in your code, and debug the data structures for deleg and expr. You should see that expr contains a lot of information about the C# code that makes the actual expression. A compiled expression is not different from the regular Func, but compiling it takes more memory and time to process, so if you're not going to do fancy stuff with it (like Entity Framework does), it serves no purpose.

    I hope this makes sense?


    Kind regards,

    Johnny Hooyberghs

    Wednesday, July 31, 2019 2:18 PM
  • Hi wakefun, 

    Thank you for posting here.

    For you question, you want to know more about Expression Tree.

    Actually, Expression Tree is a kind of data structure and has different compilations comparing to delegate.

    Here’s a sample of Expression Tree.

               Expression<Func<int, int, int>> expression = 
                                    (a, b)=> a * b + 2;
    

    Following is the data structure.


    We need Expression because it is much easier to translate a data structure such as an expression tree into SQL than it is to translate raw IL or executable code into SQL.

    Besides, I have found two useful references.

    The first reference explain reasons to use Expression<Func<T>>.

    Why would you use Expression<Func<T>> rather than Func<T>?

    The second reference can help you understand Expression Tree.

    Expression Tree Basics

    Hope them can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by wakefun Wednesday, August 7, 2019 8:13 AM
    Thursday, August 1, 2019 8:44 AM
    Moderator