Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Survey - Who here uses System.Linq.Expressions to build Lambdas?

Survey - Who here uses System.Linq.Expressions to build Lambdas?

Scheduled Pinned Locked Moved The Lounge
linqcsharpcomdata-structuresfunctional
31 Posts 12 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Andrew Rissing

    I'm just curious, so I wanted to take a quick poll to see how many people have actually used or are currently using System.Linq.Expressions[^]? Basically, have you written code like this in your life (courtesy of MSDN[^])?

    // Add the following directive to the file:
    // using System.Linq.Expressions;

    // An expression that represents the switch value.
    ConstantExpression switchValue = Expression.Constant(3);

    // This expression represents a switch statement
    // that has a default case.
    SwitchExpression switchExpr =
    Expression.Switch(
    switchValue,
    Expression.Call(
    null,
    typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
    Expression.Constant("Default")
    ),
    new SwitchCase[] {
    Expression.SwitchCase(
    Expression.Call(
    null,
    typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
    Expression.Constant("First")
    ),
    Expression.Constant(1)
    ),
    Expression.SwitchCase(
    Expression.Call(
    null,
    typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
    Expression.Constant("Second")
    ),
    Expression.Constant(2)
    )
    }
    );

    // The following statement first creates an expression tree,
    // then compiles it, and then runs it.
    Expression.Lambda(switchExpr).Compile()();

    // This code example produces the following output:
    //
    // Default

    Update: For those that use other techniques (CodeDom, Emit, etc.), would you use Expressions more if it was easier to use? If not, why would you choose to stay with your current technique?

    P Offline
    P Offline
    Phil Martin
    wrote on last edited by
    #6

    Cool snippet. I'll hopefully find a way to use something similar in the near future. I haven't used Expressions to build lambdas, but I have used CodeDom a lot, and it is about 17,000 times uglier than the code you pasted. I feel dirty even thinking about it.

    A 1 Reply Last reply
    0
    • A Andrew Rissing

      I'm just curious, so I wanted to take a quick poll to see how many people have actually used or are currently using System.Linq.Expressions[^]? Basically, have you written code like this in your life (courtesy of MSDN[^])?

      // Add the following directive to the file:
      // using System.Linq.Expressions;

      // An expression that represents the switch value.
      ConstantExpression switchValue = Expression.Constant(3);

      // This expression represents a switch statement
      // that has a default case.
      SwitchExpression switchExpr =
      Expression.Switch(
      switchValue,
      Expression.Call(
      null,
      typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
      Expression.Constant("Default")
      ),
      new SwitchCase[] {
      Expression.SwitchCase(
      Expression.Call(
      null,
      typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
      Expression.Constant("First")
      ),
      Expression.Constant(1)
      ),
      Expression.SwitchCase(
      Expression.Call(
      null,
      typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
      Expression.Constant("Second")
      ),
      Expression.Constant(2)
      )
      }
      );

      // The following statement first creates an expression tree,
      // then compiles it, and then runs it.
      Expression.Lambda(switchExpr).Compile()();

      // This code example produces the following output:
      //
      // Default

      Update: For those that use other techniques (CodeDom, Emit, etc.), would you use Expressions more if it was easier to use? If not, why would you choose to stay with your current technique?

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #7

      I use System.Linq extensively, but not System.Linq.Expressions. /ravi

      My new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com

      1 Reply Last reply
      0
      • A Andrew Rissing

        I'm just curious, so I wanted to take a quick poll to see how many people have actually used or are currently using System.Linq.Expressions[^]? Basically, have you written code like this in your life (courtesy of MSDN[^])?

        // Add the following directive to the file:
        // using System.Linq.Expressions;

        // An expression that represents the switch value.
        ConstantExpression switchValue = Expression.Constant(3);

        // This expression represents a switch statement
        // that has a default case.
        SwitchExpression switchExpr =
        Expression.Switch(
        switchValue,
        Expression.Call(
        null,
        typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
        Expression.Constant("Default")
        ),
        new SwitchCase[] {
        Expression.SwitchCase(
        Expression.Call(
        null,
        typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
        Expression.Constant("First")
        ),
        Expression.Constant(1)
        ),
        Expression.SwitchCase(
        Expression.Call(
        null,
        typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
        Expression.Constant("Second")
        ),
        Expression.Constant(2)
        )
        }
        );

        // The following statement first creates an expression tree,
        // then compiles it, and then runs it.
        Expression.Lambda(switchExpr).Compile()();

        // This code example produces the following output:
        //
        // Default

        Update: For those that use other techniques (CodeDom, Emit, etc.), would you use Expressions more if it was easier to use? If not, why would you choose to stay with your current technique?

        B Offline
        B Offline
        BobJanova
        wrote on last edited by
        #8

        Never. I have some System.Reflection.Emit code for dynamically generating statically typed delegates (for attaching to things), and lots of reflective stuff, as I wrote a language interpreter which needs to do all that stuff at runtime.

        A 1 Reply Last reply
        0
        • P Phil Martin

          Cool snippet. I'll hopefully find a way to use something similar in the near future. I haven't used Expressions to build lambdas, but I have used CodeDom a lot, and it is about 17,000 times uglier than the code you pasted. I feel dirty even thinking about it.

          A Offline
          A Offline
          Andrew Rissing
          wrote on last edited by
          #9

          If using Expressions was easier, would you likely use it over CodeDom? If not, what is the reason for your usage of CodeDom vs. Expressions?

          P 1 Reply Last reply
          0
          • A Andrew Rissing

            I'm just curious, so I wanted to take a quick poll to see how many people have actually used or are currently using System.Linq.Expressions[^]? Basically, have you written code like this in your life (courtesy of MSDN[^])?

            // Add the following directive to the file:
            // using System.Linq.Expressions;

            // An expression that represents the switch value.
            ConstantExpression switchValue = Expression.Constant(3);

            // This expression represents a switch statement
            // that has a default case.
            SwitchExpression switchExpr =
            Expression.Switch(
            switchValue,
            Expression.Call(
            null,
            typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
            Expression.Constant("Default")
            ),
            new SwitchCase[] {
            Expression.SwitchCase(
            Expression.Call(
            null,
            typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
            Expression.Constant("First")
            ),
            Expression.Constant(1)
            ),
            Expression.SwitchCase(
            Expression.Call(
            null,
            typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
            Expression.Constant("Second")
            ),
            Expression.Constant(2)
            )
            }
            );

            // The following statement first creates an expression tree,
            // then compiles it, and then runs it.
            Expression.Lambda(switchExpr).Compile()();

            // This code example produces the following output:
            //
            // Default

            Update: For those that use other techniques (CodeDom, Emit, etc.), would you use Expressions more if it was easier to use? If not, why would you choose to stay with your current technique?

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #10

            I feel dirty for admitting that I have. There's a piece of code that we have that does this for generating messaging layers based on config that we pass in - it's a nasty piece of code that seemed cool at the time - now I'm afraid to touch it because of what it could break.

            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

            A N 2 Replies Last reply
            0
            • A Andrew Rissing

              I'm just curious, so I wanted to take a quick poll to see how many people have actually used or are currently using System.Linq.Expressions[^]? Basically, have you written code like this in your life (courtesy of MSDN[^])?

              // Add the following directive to the file:
              // using System.Linq.Expressions;

              // An expression that represents the switch value.
              ConstantExpression switchValue = Expression.Constant(3);

              // This expression represents a switch statement
              // that has a default case.
              SwitchExpression switchExpr =
              Expression.Switch(
              switchValue,
              Expression.Call(
              null,
              typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
              Expression.Constant("Default")
              ),
              new SwitchCase[] {
              Expression.SwitchCase(
              Expression.Call(
              null,
              typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
              Expression.Constant("First")
              ),
              Expression.Constant(1)
              ),
              Expression.SwitchCase(
              Expression.Call(
              null,
              typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
              Expression.Constant("Second")
              ),
              Expression.Constant(2)
              )
              }
              );

              // The following statement first creates an expression tree,
              // then compiles it, and then runs it.
              Expression.Lambda(switchExpr).Compile()();

              // This code example produces the following output:
              //
              // Default

              Update: For those that use other techniques (CodeDom, Emit, etc.), would you use Expressions more if it was easier to use? If not, why would you choose to stay with your current technique?

              T Offline
              T Offline
              TheGreatAndPowerfulOz
              wrote on last edited by
              #11

              Occasionally, especially when I need fast Reflection.

              If your actions inspire others to dream more, learn more, do more and become more, you are a leader.-John Q. Adams
              You must accept one of two basic premises: Either we are alone in the universe, or we are not alone in the universe. And either way, the implications are staggering.-Wernher von Braun
              Only two things are infinite, the universe and human stupidity, and I'm not sure about the former.-Albert Einstein

              1 Reply Last reply
              0
              • B BobJanova

                Never. I have some System.Reflection.Emit code for dynamically generating statically typed delegates (for attaching to things), and lots of reflective stuff, as I wrote a language interpreter which needs to do all that stuff at runtime.

                A Offline
                A Offline
                Andrew Rissing
                wrote on last edited by
                #12

                (Same question I asked Phil) If using Expressions was easier, would you likely use it over Emit? If not, what is the reason for your usage of Emit vs. Expressions?

                B 1 Reply Last reply
                0
                • P Pete OHanlon

                  I feel dirty for admitting that I have. There's a piece of code that we have that does this for generating messaging layers based on config that we pass in - it's a nasty piece of code that seemed cool at the time - now I'm afraid to touch it because of what it could break.

                  *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                  "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                  CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                  A Offline
                  A Offline
                  Andrew Rissing
                  wrote on last edited by
                  #13

                  *grin* It is nasty because the code is a tangled mess itself or is it just the interconnectedness of it all?

                  P 1 Reply Last reply
                  0
                  • R Rhys Gravell

                    Yes I have, yes I do, basic example;

                    public Society FindById(int id)
                    {
                    return this.FirstOrDefault(item => item.Id.Equals(id));
                    }

                    Rhys "Technological progress is like an axe in the hands of a pathological criminal" "Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"

                    N Offline
                    N Offline
                    NormDroid
                    wrote on last edited by
                    #14

                    That seems perfectly fine to me.

                    Software Kinetics Dependable Software

                    1 Reply Last reply
                    0
                    • A Andrew Rissing

                      *grin* It is nasty because the code is a tangled mess itself or is it just the interconnectedness of it all?

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #15

                      A bit of both really. It replaced a CodeDOM module that we had - the problem is, we attempted to go feature for feature with the CodeDOM stuff, rather than taking the time to revisit the whole architecture. Now it's so convoluted that I don't want to touch it. Sooner or later we'll have to bite the bullet, but there are other more pressing things to work on first, and I really don't want to divert resources to this just in case they become suicidal.

                      *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                      "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                      CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                      1 Reply Last reply
                      0
                      • A Andrew Rissing

                        (Same question I asked Phil) If using Expressions was easier, would you likely use it over Emit? If not, what is the reason for your usage of Emit vs. Expressions?

                        B Offline
                        B Offline
                        BobJanova
                        wrote on last edited by
                        #16

                        I wrote this code under .Net 1.1. I'm not sure Expressions can build delegates, though, can it? And yeah I'd use anything over Emit that did the same job. Writing out IL opcodes is not at all fast or easy!

                        A 1 Reply Last reply
                        0
                        • B BobJanova

                          I wrote this code under .Net 1.1. I'm not sure Expressions can build delegates, though, can it? And yeah I'd use anything over Emit that did the same job. Writing out IL opcodes is not at all fast or easy!

                          A Offline
                          A Offline
                          Andrew Rissing
                          wrote on last edited by
                          #17

                          Yes, Expressions can build delegates (typed even). Take a look at this[^]. The reason for this survey is that I've developed an open source API to make expressions easier. I'm just trying to gauge how useful it would be to the community at large. Once I've added some unit tests, I'll put together an article and post it on CodeProject. :D

                          P 1 Reply Last reply
                          0
                          • A Andrew Rissing

                            Yes, Expressions can build delegates (typed even). Take a look at this[^]. The reason for this survey is that I've developed an open source API to make expressions easier. I'm just trying to gauge how useful it would be to the community at large. Once I've added some unit tests, I'll put together an article and post it on CodeProject. :D

                            P Offline
                            P Offline
                            Pete OHanlon
                            wrote on last edited by
                            #18

                            I'll certainly take a look at it.

                            *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                            "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                            CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                            A 1 Reply Last reply
                            0
                            • A Andrew Rissing

                              If using Expressions was easier, would you likely use it over CodeDom? If not, what is the reason for your usage of CodeDom vs. Expressions?

                              P Offline
                              P Offline
                              Phil Martin
                              wrote on last edited by
                              #19

                              The short answer is that I couldn't figure out a way for Expressions to write out c# code to a file.

                              A 1 Reply Last reply
                              0
                              • A Andrew Rissing

                                I'm just curious, so I wanted to take a quick poll to see how many people have actually used or are currently using System.Linq.Expressions[^]? Basically, have you written code like this in your life (courtesy of MSDN[^])?

                                // Add the following directive to the file:
                                // using System.Linq.Expressions;

                                // An expression that represents the switch value.
                                ConstantExpression switchValue = Expression.Constant(3);

                                // This expression represents a switch statement
                                // that has a default case.
                                SwitchExpression switchExpr =
                                Expression.Switch(
                                switchValue,
                                Expression.Call(
                                null,
                                typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
                                Expression.Constant("Default")
                                ),
                                new SwitchCase[] {
                                Expression.SwitchCase(
                                Expression.Call(
                                null,
                                typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
                                Expression.Constant("First")
                                ),
                                Expression.Constant(1)
                                ),
                                Expression.SwitchCase(
                                Expression.Call(
                                null,
                                typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
                                Expression.Constant("Second")
                                ),
                                Expression.Constant(2)
                                )
                                }
                                );

                                // The following statement first creates an expression tree,
                                // then compiles it, and then runs it.
                                Expression.Lambda(switchExpr).Compile()();

                                // This code example produces the following output:
                                //
                                // Default

                                Update: For those that use other techniques (CodeDom, Emit, etc.), would you use Expressions more if it was easier to use? If not, why would you choose to stay with your current technique?

                                A Offline
                                A Offline
                                AspDotNetDev
                                wrote on last edited by
                                #20

                                I tried something like that, but failed, so I bought a book on LINQ and haven't had a chance to read it yet. That is some confusing stuff.

                                Thou mewling ill-breeding pignut!

                                A 1 Reply Last reply
                                0
                                • P Phil Martin

                                  The short answer is that I couldn't figure out a way for Expressions to write out c# code to a file.

                                  A Offline
                                  A Offline
                                  Andrew Rissing
                                  wrote on last edited by
                                  #21

                                  You needed to write it out to a file?

                                  1 Reply Last reply
                                  0
                                  • P Pete OHanlon

                                    I'll certainly take a look at it.

                                    *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

                                    "Mind bleach! Send me mind bleach!" - Nagy Vilmos

                                    CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

                                    A Offline
                                    A Offline
                                    Andrew Rissing
                                    wrote on last edited by
                                    #22

                                    The link or the API I'm referring to? :D

                                    P 1 Reply Last reply
                                    0
                                    • A AspDotNetDev

                                      I tried something like that, but failed, so I bought a book on LINQ and haven't had a chance to read it yet. That is some confusing stuff.

                                      Thou mewling ill-breeding pignut!

                                      A Offline
                                      A Offline
                                      Andrew Rissing
                                      wrote on last edited by
                                      #23

                                      What I'm working on might be of use to you, once I wrap it up in unit testing and an article for the site then. Btw, you probably would love to have seen this[^] back then. Static reflection using Expression trees is quite nice with something like what I linked.

                                      A 1 Reply Last reply
                                      0
                                      • A Andrew Rissing

                                        What I'm working on might be of use to you, once I wrap it up in unit testing and an article for the site then. Btw, you probably would love to have seen this[^] back then. Static reflection using Expression trees is quite nice with something like what I linked.

                                        A Offline
                                        A Offline
                                        AspDotNetDev
                                        wrote on last edited by
                                        #24

                                        Certainly looks useful! I'll tuck that away for whenever I get back to doing complex LINQ stuff. :)

                                        Thou mewling ill-breeding pignut!

                                        1 Reply Last reply
                                        0
                                        • A Andrew Rissing

                                          I'm just curious, so I wanted to take a quick poll to see how many people have actually used or are currently using System.Linq.Expressions[^]? Basically, have you written code like this in your life (courtesy of MSDN[^])?

                                          // Add the following directive to the file:
                                          // using System.Linq.Expressions;

                                          // An expression that represents the switch value.
                                          ConstantExpression switchValue = Expression.Constant(3);

                                          // This expression represents a switch statement
                                          // that has a default case.
                                          SwitchExpression switchExpr =
                                          Expression.Switch(
                                          switchValue,
                                          Expression.Call(
                                          null,
                                          typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
                                          Expression.Constant("Default")
                                          ),
                                          new SwitchCase[] {
                                          Expression.SwitchCase(
                                          Expression.Call(
                                          null,
                                          typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
                                          Expression.Constant("First")
                                          ),
                                          Expression.Constant(1)
                                          ),
                                          Expression.SwitchCase(
                                          Expression.Call(
                                          null,
                                          typeof(Console).GetMethod("WriteLine", new Type[] { typeof(String) }),
                                          Expression.Constant("Second")
                                          ),
                                          Expression.Constant(2)
                                          )
                                          }
                                          );

                                          // The following statement first creates an expression tree,
                                          // then compiles it, and then runs it.
                                          Expression.Lambda(switchExpr).Compile()();

                                          // This code example produces the following output:
                                          //
                                          // Default

                                          Update: For those that use other techniques (CodeDom, Emit, etc.), would you use Expressions more if it was easier to use? If not, why would you choose to stay with your current technique?

                                          J Offline
                                          J Offline
                                          jschell
                                          wrote on last edited by
                                          #25

                                          Andrew Rissing wrote:

                                          Basically, have you written code like this in your life

                                          No.

                                          Andrew Rissing wrote:

                                          would you use Expressions more if it was easier to use?

                                          No.

                                          Andrew Rissing wrote:

                                          If not, why would you choose to stay with your current technique?

                                          The only common usage I have seen for linq is for database access. And I either generate code like that or use dynamic framework apis. The only other usages I have seen have all been of the "gee this is really cool so lets find some way to use it regardless of how inappropriate it is".

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups