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. Other Discussions
  3. Clever Code
  4. Enumerable.Except

Enumerable.Except

Scheduled Pinned Locked Moved Clever Code
csharplinqcomquestion
13 Posts 6 Posters 42 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.
  • P PIEBALDconsult

    More reason not to use Linq.

    J Offline
    J Offline
    Judah Gabriel Himango
    wrote on last edited by
    #3

    Nah. LINQ saves us from so many headaches and ugly procedural code. We really love it here. It's just that there's some gotchas to watch out for.

    Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

    N 1 Reply Last reply
    0
    • J Judah Gabriel Himango

      A coworker found this rather surprising aspect of the Enumerable.Except LINQ extension method:

      var items = new [] { 10, 10 };
      var result = items.Except(42);

      What would you expect the result to be? We expected result to be a sequence of {10, 10}. Surprise! It's { 10 }; Except apparently removes all non-distinct items from the sequence, in addition to the elements passed in. X|

      Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

      N Offline
      N Offline
      Nish Nishant
      wrote on last edited by
      #4

      Hey Judah, Except returns the set difference, which is well documented, so I don't see how this is surprising. I think your coworker just did not understand what the method does.

      Regards, Nish


      Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
      My latest book : C++/CLI in Action / Amazon.com link

      J 1 Reply Last reply
      0
      • P PIEBALDconsult

        More reason not to use Linq.

        N Offline
        N Offline
        Nish Nishant
        wrote on last edited by
        #5

        PIEBALDconsult wrote:

        More reason not to use Linq.

        What would the other reasons be?

        Regards, Nish


        Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
        My latest book : C++/CLI in Action / Amazon.com link

        1 Reply Last reply
        0
        • J Judah Gabriel Himango

          Nah. LINQ saves us from so many headaches and ugly procedural code. We really love it here. It's just that there's some gotchas to watch out for.

          Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

          N Offline
          N Offline
          Nish Nishant
          wrote on last edited by
          #6

          Judah Himango wrote:

          It's just that there's some gotchas to watch out for.

          One mistake I've made in the past that I've seen other people make too is a tendency to overuse it without thinking of performance or whether there are simpler ways to do it. Like people blindly calling ToList, ToArray etc. Just because it's so easy and doable does not mean you should. On a side note after this thread of yours, we are going to have to strip you off the title of CP's resident Linq Evangelist :-D

          Regards, Nish


          Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
          My latest book : C++/CLI in Action / Amazon.com link

          J 1 Reply Last reply
          0
          • N Nish Nishant

            Hey Judah, Except returns the set difference, which is well documented, so I don't see how this is surprising. I think your coworker just did not understand what the method does.

            Regards, Nish


            Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
            My latest book : C++/CLI in Action / Amazon.com link

            J Offline
            J Offline
            Judah Gabriel Himango
            wrote on last edited by
            #7

            Yeah, MSDN says "set difference". It's just not what I'd expect a method named "Except" to do. I'd expect Except to do something like this:

            public IEnumerable<T> Except(this IEnumerable<T> sequence, IEnumerable<T> items)
            {
            foreach(var item in sequence)
            {
            if (!items.Contains(item)) yield return item;
            }
            }

            Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

            J D 2 Replies Last reply
            0
            • N Nish Nishant

              Judah Himango wrote:

              It's just that there's some gotchas to watch out for.

              One mistake I've made in the past that I've seen other people make too is a tendency to overuse it without thinking of performance or whether there are simpler ways to do it. Like people blindly calling ToList, ToArray etc. Just because it's so easy and doable does not mean you should. On a side note after this thread of yours, we are going to have to strip you off the title of CP's resident Linq Evangelist :-D

              Regards, Nish


              Nish’s thoughts on MFC, C++/CLI and .NET (my blog)
              My latest book : C++/CLI in Action / Amazon.com link

              J Offline
              J Offline
              Judah Gabriel Himango
              wrote on last edited by
              #8

              Nishant Sivakumar wrote:

              On a side note after this thread of yours, we are going to have to strip you off the title of CP's resident Linq Evangelist

              :laugh: I didn't know I was the LINQ evangelist of CP. :cool::thumbsup: But I just know the basics; I discovered the "into" keyword just the other day, for example. So I'm basically a newb.

              Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

              1 Reply Last reply
              0
              • P PIEBALDconsult

                More reason not to use Linq.

                N Offline
                N Offline
                Nemanja Trifunovic
                wrote on last edited by
                #9

                PIEBALDconsult wrote:

                More reason not to use Linq

                1.00/5 (4 votes) A popular opinion, isn't it?

                Programming Blog utf8-cpp

                P 1 Reply Last reply
                0
                • N Nemanja Trifunovic

                  PIEBALDconsult wrote:

                  More reason not to use Linq

                  1.00/5 (4 votes) A popular opinion, isn't it?

                  Programming Blog utf8-cpp

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

                  Obviously I don't say things for popularity. :rolleyes:

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    More reason not to use Linq.

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

                    Well, if you know how it works, then you have all the reasons to use it. Linq to SQL is another case though. If you work with more than a handful of records/objects at a time, the performance is just horrible. I had to rewrite a lot of code because of its inefficiencies. In some cases, the new and improved code was 70 times(!) faster.

                    -- Kein Mitleid Für Die Mehrheit

                    1 Reply Last reply
                    0
                    • J Judah Gabriel Himango

                      Yeah, MSDN says "set difference". It's just not what I'd expect a method named "Except" to do. I'd expect Except to do something like this:

                      public IEnumerable<T> Except(this IEnumerable<T> sequence, IEnumerable<T> items)
                      {
                      foreach(var item in sequence)
                      {
                      if (!items.Contains(item)) yield return item;
                      }
                      }

                      Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

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

                      I agree. It ought to have been named SetDifference() or maybe just Difference().

                      -- Kein Mitleid Für Die Mehrheit

                      1 Reply Last reply
                      0
                      • J Judah Gabriel Himango

                        Yeah, MSDN says "set difference". It's just not what I'd expect a method named "Except" to do. I'd expect Except to do something like this:

                        public IEnumerable<T> Except(this IEnumerable<T> sequence, IEnumerable<T> items)
                        {
                        foreach(var item in sequence)
                        {
                        if (!items.Contains(item)) yield return item;
                        }
                        }

                        Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

                        D Offline
                        D Offline
                        dojohansen
                        wrote on last edited by
                        #13

                        Maybe you should add an extension method named Expect then :D

                        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