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. A programming observation...

A programming observation...

Scheduled Pinned Locked Moved The Lounge
csharpdatabasecomhelptutorial
24 Posts 11 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.
  • M Offline
    M Offline
    Miszou
    wrote on last edited by
    #1

    Imagine a set of numbers, and you need to determine if they are all unique. (Slightly contrived example, but it'll work for now). In C# for example, you might do a simple foreach loop and break/set a flag if the previous number was different from the current one. But I had to do this in SQL, and for about 30 seconds, my brain was in the looping/counting mode of C#, and what a ghastly mess I was going to end up with... Then suddenly my brain engaged itself fully, slipped into SQL mode and I realized (of course) that I could do exactly the same thing with a trivial select count( distinct id ) from #t, where a result of anything greater than 1 would indicate that the numbers were not unique. I'm not sure why I posted this, other than to marvel at two completely different methods of accomplishing the exact same thing. I'm sure there are hundreds of ways of solving this simple problem, and hundreds more languages in which to do so. Many of them I imagine would be similar, but some of the more esoteric methods/languages could be really interesting. :cool: Anyway, just something to think about...

    The StartPage Randomizer - The Windows Cheerleader - Twitter

    A M J R 4 Replies Last reply
    0
    • M Miszou

      Imagine a set of numbers, and you need to determine if they are all unique. (Slightly contrived example, but it'll work for now). In C# for example, you might do a simple foreach loop and break/set a flag if the previous number was different from the current one. But I had to do this in SQL, and for about 30 seconds, my brain was in the looping/counting mode of C#, and what a ghastly mess I was going to end up with... Then suddenly my brain engaged itself fully, slipped into SQL mode and I realized (of course) that I could do exactly the same thing with a trivial select count( distinct id ) from #t, where a result of anything greater than 1 would indicate that the numbers were not unique. I'm not sure why I posted this, other than to marvel at two completely different methods of accomplishing the exact same thing. I'm sure there are hundreds of ways of solving this simple problem, and hundreds more languages in which to do so. Many of them I imagine would be similar, but some of the more esoteric methods/languages could be really interesting. :cool: Anyway, just something to think about...

      The StartPage Randomizer - The Windows Cheerleader - Twitter

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

      Depending on how SQL is implemented under the covers, it might be doing a loop itself. Although I imagine it has optimizations built in to do a dictionary-like lookup, which could also be done relatively easy in C# (probably even faster than sorting the data first). Heck, they might even be really similar looking with the advent of LINQ. Guess SQL is just more geared towards handling sets of data.

      Visual Studio is an excellent GUIIDE.

      1 Reply Last reply
      0
      • M Miszou

        Imagine a set of numbers, and you need to determine if they are all unique. (Slightly contrived example, but it'll work for now). In C# for example, you might do a simple foreach loop and break/set a flag if the previous number was different from the current one. But I had to do this in SQL, and for about 30 seconds, my brain was in the looping/counting mode of C#, and what a ghastly mess I was going to end up with... Then suddenly my brain engaged itself fully, slipped into SQL mode and I realized (of course) that I could do exactly the same thing with a trivial select count( distinct id ) from #t, where a result of anything greater than 1 would indicate that the numbers were not unique. I'm not sure why I posted this, other than to marvel at two completely different methods of accomplishing the exact same thing. I'm sure there are hundreds of ways of solving this simple problem, and hundreds more languages in which to do so. Many of them I imagine would be similar, but some of the more esoteric methods/languages could be really interesting. :cool: Anyway, just something to think about...

        The StartPage Randomizer - The Windows Cheerleader - Twitter

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        When Linq first raised it's head I was delighted, here is TSQL for datatables in C#, I'll be able to do queries against a datatable in the business logic layer without resorting to the database. What a frikking nightmare we have ended up with. I like TSQL and its brother PLSQL, I know SQL syntax, I'm comfortable with SQL, it is a tried and true way of manipulating data. If I could do that in C# it would save me endless time, but no we get Linq. What a PITA.

        P N 2 Replies Last reply
        0
        • M Miszou

          Imagine a set of numbers, and you need to determine if they are all unique. (Slightly contrived example, but it'll work for now). In C# for example, you might do a simple foreach loop and break/set a flag if the previous number was different from the current one. But I had to do this in SQL, and for about 30 seconds, my brain was in the looping/counting mode of C#, and what a ghastly mess I was going to end up with... Then suddenly my brain engaged itself fully, slipped into SQL mode and I realized (of course) that I could do exactly the same thing with a trivial select count( distinct id ) from #t, where a result of anything greater than 1 would indicate that the numbers were not unique. I'm not sure why I posted this, other than to marvel at two completely different methods of accomplishing the exact same thing. I'm sure there are hundreds of ways of solving this simple problem, and hundreds more languages in which to do so. Many of them I imagine would be similar, but some of the more esoteric methods/languages could be really interesting. :cool: Anyway, just something to think about...

          The StartPage Randomizer - The Windows Cheerleader - Twitter

          J Offline
          J Offline
          J Dunlap
          wrote on last edited by
          #4

          Check whether all IDs are the same via extension methods in C#:

          var first = objs.First();
          bool nonUnique = objs.Any(o=>o.ID!=first.ID);

          Total unique count in C# via my DistinctBy() extension method:

          int count = objs.DistinctBy(o=> o.ID).Count();

          :cool:

          M 1 Reply Last reply
          0
          • M Mycroft Holmes

            When Linq first raised it's head I was delighted, here is TSQL for datatables in C#, I'll be able to do queries against a datatable in the business logic layer without resorting to the database. What a frikking nightmare we have ended up with. I like TSQL and its brother PLSQL, I know SQL syntax, I'm comfortable with SQL, it is a tried and true way of manipulating data. If I could do that in C# it would save me endless time, but no we get Linq. What a PITA.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            10!

            Mycroft Holmes wrote:

            do queries against a datatable

            I don't know why I'd want to do that.

            1 Reply Last reply
            0
            • M Mycroft Holmes

              When Linq first raised it's head I was delighted, here is TSQL for datatables in C#, I'll be able to do queries against a datatable in the business logic layer without resorting to the database. What a frikking nightmare we have ended up with. I like TSQL and its brother PLSQL, I know SQL syntax, I'm comfortable with SQL, it is a tried and true way of manipulating data. If I could do that in C# it would save me endless time, but no we get Linq. What a PITA.

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              It looks like you have understanding problem of what Linq does. Most people thinks Linq to SQL is the only possibility that Linq offers. Linq is much more than that and it's not an equivalent of TSql. I like it and IMO, it produces more readable code. The only disadvantage I felt with Linq is, it makes tough to calculate the algorithm complexity. Improper use of Linq queries can result in worst complexity (I am not talking about Linq to Sql). If you are interested, take a look at this wiki[^].

              Mycroft Holmes wrote:

              I'll be able to do queries against a datatable in the business logic layer without resorting to the database.

              :confused:

              Best wishes, Navaneeth

              M M 2 Replies Last reply
              0
              • N N a v a n e e t h

                It looks like you have understanding problem of what Linq does. Most people thinks Linq to SQL is the only possibility that Linq offers. Linq is much more than that and it's not an equivalent of TSql. I like it and IMO, it produces more readable code. The only disadvantage I felt with Linq is, it makes tough to calculate the algorithm complexity. Improper use of Linq queries can result in worst complexity (I am not talking about Linq to Sql). If you are interested, take a look at this wiki[^].

                Mycroft Holmes wrote:

                I'll be able to do queries against a datatable in the business logic layer without resorting to the database.

                :confused:

                Best wishes, Navaneeth

                M Offline
                M Offline
                Mycroft Holmes
                wrote on last edited by
                #7

                N a v a n e e t h wrote:

                If you are interested, take a look at this wiki[^].

                One of my reference sites - there are a lot of them. The main problem may have been my expectation, to me SQL is simple, having used it for so many years. I was looking forward to being able to do a select sum() against a list of objects or a datatable. The syntax and debugging of Linq queries is just painful and if you throw in lambda expressions then it is even worse. I know most of these things can be done but it is a very different paradigm and certainly is not simple. Try and OUTER JOIN bah!

                1 Reply Last reply
                0
                • N N a v a n e e t h

                  It looks like you have understanding problem of what Linq does. Most people thinks Linq to SQL is the only possibility that Linq offers. Linq is much more than that and it's not an equivalent of TSql. I like it and IMO, it produces more readable code. The only disadvantage I felt with Linq is, it makes tough to calculate the algorithm complexity. Improper use of Linq queries can result in worst complexity (I am not talking about Linq to Sql). If you are interested, take a look at this wiki[^].

                  Mycroft Holmes wrote:

                  I'll be able to do queries against a datatable in the business logic layer without resorting to the database.

                  :confused:

                  Best wishes, Navaneeth

                  M Offline
                  M Offline
                  Member 96
                  wrote on last edited by
                  #8

                  N a v a n e e t h wrote:

                  it produces more readable code

                  Yeah right. :)


                  "Creating your own blog is about as easy as creating your own urine, and you're about as likely to find someone else interested in it." -- Lore Sjöberg

                  1 Reply Last reply
                  0
                  • J J Dunlap

                    Check whether all IDs are the same via extension methods in C#:

                    var first = objs.First();
                    bool nonUnique = objs.Any(o=>o.ID!=first.ID);

                    Total unique count in C# via my DistinctBy() extension method:

                    int count = objs.DistinctBy(o=> o.ID).Count();

                    :cool:

                    M Offline
                    M Offline
                    Member 96
                    wrote on last edited by
                    #9

                    Yikes. I don't know what that is and I don't want to know as it appears to be one of those modern sytactic sugar type things designed save a few minutes of typing at the complete sacrifice of maintainability and readability.


                    "Creating your own blog is about as easy as creating your own urine, and you're about as likely to find someone else interested in it." -- Lore Sjöberg

                    R B 2 Replies Last reply
                    0
                    • M Member 96

                      Yikes. I don't know what that is and I don't want to know as it appears to be one of those modern sytactic sugar type things designed save a few minutes of typing at the complete sacrifice of maintainability and readability.


                      "Creating your own blog is about as easy as creating your own urine, and you're about as likely to find someone else interested in it." -- Lore Sjöberg

                      R Offline
                      R Offline
                      Rama Krishna Vavilala
                      wrote on last edited by
                      #10

                      and what is the maintainable solution?

                      R 1 Reply Last reply
                      0
                      • M Member 96

                        Yikes. I don't know what that is and I don't want to know as it appears to be one of those modern sytactic sugar type things designed save a few minutes of typing at the complete sacrifice of maintainability and readability.


                        "Creating your own blog is about as easy as creating your own urine, and you're about as likely to find someone else interested in it." -- Lore Sjöberg

                        B Offline
                        B Offline
                        Brady Kelly
                        wrote on last edited by
                        #11

                        The second example is a really elegant and simple solution to the issue posed by Miszou. Just because you don't understand it doesn't make it at all less maintainable or readable to someone current with the .NET framework. The only syntactic sugar is the lambda expression, which is a lot more readable and maintainable than an anonymous delegate.

                        M 1 Reply Last reply
                        0
                        • M Miszou

                          Imagine a set of numbers, and you need to determine if they are all unique. (Slightly contrived example, but it'll work for now). In C# for example, you might do a simple foreach loop and break/set a flag if the previous number was different from the current one. But I had to do this in SQL, and for about 30 seconds, my brain was in the looping/counting mode of C#, and what a ghastly mess I was going to end up with... Then suddenly my brain engaged itself fully, slipped into SQL mode and I realized (of course) that I could do exactly the same thing with a trivial select count( distinct id ) from #t, where a result of anything greater than 1 would indicate that the numbers were not unique. I'm not sure why I posted this, other than to marvel at two completely different methods of accomplishing the exact same thing. I'm sure there are hundreds of ways of solving this simple problem, and hundreds more languages in which to do so. Many of them I imagine would be similar, but some of the more esoteric methods/languages could be really interesting. :cool: Anyway, just something to think about...

                          The StartPage Randomizer - The Windows Cheerleader - Twitter

                          R Offline
                          R Offline
                          realJSOP
                          wrote on last edited by
                          #12

                          You can do that with LINQ too, right?

                          .45 ACP - because shooting twice is just silly
                          -----
                          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                          -----
                          "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                          B J 2 Replies Last reply
                          0
                          • R Rama Krishna Vavilala

                            and what is the maintainable solution?

                            R Offline
                            R Offline
                            realJSOP
                            wrote on last edited by
                            #13

                            Hire a contractor to write a proper method - with comments - and no goto's. :)

                            .45 ACP - because shooting twice is just silly
                            -----
                            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                            -----
                            "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                            B 1 Reply Last reply
                            0
                            • B Brady Kelly

                              The second example is a really elegant and simple solution to the issue posed by Miszou. Just because you don't understand it doesn't make it at all less maintainable or readable to someone current with the .NET framework. The only syntactic sugar is the lambda expression, which is a lot more readable and maintainable than an anonymous delegate.

                              M Offline
                              M Offline
                              Member 96
                              wrote on last edited by
                              #14

                              It may be clever but it's far from elegant or simple and has no place in professionally written code unless you think dense, unreadable, no more performant code that can't be understood by anyone with a programming background in any language who may be brought in to maintain it in future has a place in a professional shop.


                              "Creating your own blog is about as easy as creating your own urine, and you're about as likely to find someone else interested in it." -- Lore Sjöberg

                              B 1 Reply Last reply
                              0
                              • M Member 96

                                It may be clever but it's far from elegant or simple and has no place in professionally written code unless you think dense, unreadable, no more performant code that can't be understood by anyone with a programming background in any language who may be brought in to maintain it in future has a place in a professional shop.


                                "Creating your own blog is about as easy as creating your own urine, and you're about as likely to find someone else interested in it." -- Lore Sjöberg

                                B Offline
                                B Offline
                                Brady Kelly
                                wrote on last edited by
                                #15

                                It borrows heavily from widely accepted concepts used in functional languages as well as basic set theory. Pretty soon "a programming background in any language" will include C#3, which plays a significant role in exposing the set based extension method library that is the bulk of pure LINQ. The future will yield more and more C# developers versed in LINQ and more exposed to the functional concept of programming. I utterly fail do grasp the supposed density of this single line of code: int count = objs.DistinctBy(o=> o.ID).Count(); If the lambda is foreign to you, how about a plain objs.Distinct().Count() call on a set of records? Just how dense is that compared to

                                bool DistinctList()
                                {
                                List uniqueList = new List();
                                for (int i = 0; i < objs.Count; i++)
                                {
                                if (sortedSet[i] != sortedSet[i-1])
                                {
                                uniqueList .Add(sortedSet[i];
                                }
                                return uniqueList;
                                }

                                Which one says most about what is does in the most succinct way?

                                J M 2 Replies Last reply
                                0
                                • R realJSOP

                                  You can do that with LINQ too, right?

                                  .45 ACP - because shooting twice is just silly
                                  -----
                                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                                  -----
                                  "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                                  B Offline
                                  B Offline
                                  Brady Kelly
                                  wrote on last edited by
                                  #16

                                  Yep! :-D

                                  1 Reply Last reply
                                  0
                                  • R realJSOP

                                    Hire a contractor to write a proper method - with comments - and no goto's. :)

                                    .45 ACP - because shooting twice is just silly
                                    -----
                                    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                                    -----
                                    "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                                    B Offline
                                    B Offline
                                    Brady Kelly
                                    wrote on last edited by
                                    #17

                                    Pick me! Pick me. My solution is to generate a SQL query that uses distinct and pass the collection to that before returning the results as an IEnumerable. :cool:

                                    1 Reply Last reply
                                    0
                                    • B Brady Kelly

                                      It borrows heavily from widely accepted concepts used in functional languages as well as basic set theory. Pretty soon "a programming background in any language" will include C#3, which plays a significant role in exposing the set based extension method library that is the bulk of pure LINQ. The future will yield more and more C# developers versed in LINQ and more exposed to the functional concept of programming. I utterly fail do grasp the supposed density of this single line of code: int count = objs.DistinctBy(o=> o.ID).Count(); If the lambda is foreign to you, how about a plain objs.Distinct().Count() call on a set of records? Just how dense is that compared to

                                      bool DistinctList()
                                      {
                                      List uniqueList = new List();
                                      for (int i = 0; i < objs.Count; i++)
                                      {
                                      if (sortedSet[i] != sortedSet[i-1])
                                      {
                                      uniqueList .Add(sortedSet[i];
                                      }
                                      return uniqueList;
                                      }

                                      Which one says most about what is does in the most succinct way?

                                      J Offline
                                      J Offline
                                      Jorgen Sigvardsson
                                      wrote on last edited by
                                      #18

                                      I remember similar discussions about the ?: operator in C/C++/C# many years ago. Some people will always fear the new stuff they don't have the time to learn. I think it's fear of becoming obsolete. Also, you must not forget that John's words are law. All he says is the truth, and nothing but the truth. I for one love lambdas in C#, because they make life MUCH easier, and quite frankly, the code is more readable. No more essays, just one liners! Heck, you can even throw in a one line comment just above the code, explaining what it does (in case the lambda-impaired have to read/maintain the code).

                                      M 1 Reply Last reply
                                      0
                                      • R realJSOP

                                        You can do that with LINQ too, right?

                                        .45 ACP - because shooting twice is just silly
                                        -----
                                        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                                        -----
                                        "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

                                        J Offline
                                        J Offline
                                        Jorgen Sigvardsson
                                        wrote on last edited by
                                        #19

                                        Only an unprofessional programmer would do that, or so they say...

                                        1 Reply Last reply
                                        0
                                        • J Jorgen Sigvardsson

                                          I remember similar discussions about the ?: operator in C/C++/C# many years ago. Some people will always fear the new stuff they don't have the time to learn. I think it's fear of becoming obsolete. Also, you must not forget that John's words are law. All he says is the truth, and nothing but the truth. I for one love lambdas in C#, because they make life MUCH easier, and quite frankly, the code is more readable. No more essays, just one liners! Heck, you can even throw in a one line comment just above the code, explaining what it does (in case the lambda-impaired have to read/maintain the code).

                                          M Offline
                                          M Offline
                                          Member 96
                                          wrote on last edited by
                                          #20

                                          Jörgen Sigvardsson wrote:

                                          Also, you must not forget that John's words are law. All he says is the truth, and nothing but the truth.

                                          As I've said probably a zillion times on this site and qualified a zillion other times: we all come from our own perspective and what we say reflects that. What you're really saying here is that you don't like a forceful argument and that's a shame but not my concern.


                                          "Creating your own blog is about as easy as creating your own urine, and you're about as likely to find someone else interested in it." -- Lore Sjöberg

                                          J 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