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

    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
                • A Andrew Rissing

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

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

                  The API.

                  *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
                  • P Pete OHanlon

                    The API.

                    *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
                    #27

                    I've finished adding features at this point, so it'll likely be 1-2 weeks before an article pops up here on the site [needs unit testing and minor bug fixes]. If you want I can message you when it comes out or you'll likely just stumble upon it in your normal flow of things.

                    P 1 Reply Last reply
                    0
                    • A Andrew Rissing

                      I've finished adding features at this point, so it'll likely be 1-2 weeks before an article pops up here on the site [needs unit testing and minor bug fixes]. If you want I can message you when it comes out or you'll likely just stumble upon it in your normal flow of things.

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

                      Message me so I know - I sometimes miss things if I'm busy at work, and as this is year end, I'm going to be very busy for the next month.

                      *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
                      • 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

                        N Offline
                        N Offline
                        Nagy Vilmos
                        wrote on last edited by
                        #29

                        At one of my previous jobs there was a piece of code like that. It could be fired from any one of several events and even the most innocuousness change would break it beyond all recognition. The last change made to it was to allow upto six instead of four options. The whole thing when FUBAR and it took three weeks of very clever people, plus me, to work out WTF was happening. There was a magic number derived from a magic number from a config lookup that required the entire config to be re-written. :omg: It was a bitch-slut module and I hope the b'tard who wrote it rots in hell. When I left, there was pleading that I'd take the POS with me and support it as I was, by then, about the only person who understood it. It's still there and still in use and, as of last week, unchanged.


                        Panic, Chaos, Destruction. My work here is done. Drink. Get drunk. Fall over - P O'H OK, I will win to day or my name isn't Ethel Crudacre! - DD Ethel Crudacre I cannot live by bread alone. Bacon and ketchup are needed as well. - Trollslayer Have a bit more patience with newbies. Of course some of them act dumb - they're often *students*, for heaven's sake - Terry Pratchett

                        1 Reply Last reply
                        0
                        • P Pete OHanlon

                          Message me so I know - I sometimes miss things if I'm busy at work, and as this is year end, I'm going to be very busy for the next month.

                          *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
                          #30

                          Roger roger.

                          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
                            Jeremy Hutchinson
                            wrote on last edited by
                            #31

                            Yer, I'm using System.Linq.Expressions to build an expression tree for our policy and claim search screens. The end result is that I can have as complex of a where clause as I'd ever want to create that buildable through our front end. It really is a nice slick piece of code and it has worked flawlessly since I first created it. But if I had it to do over again, I wouldn't have used expressions. It was a pain in the ass to get working, and even I don't want to get back in there and touch it again. I can only imagine the next guys awe and/or horror when he opens that. I'd probably go with a more simple generation of a SQL where clause.

                            My Blog[^] Chess Tactics for WP7[^]

                            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