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. General Programming
  3. LINQ
  4. Except Confusion

Except Confusion

Scheduled Pinned Locked Moved LINQ
question
6 Posts 3 Posters 2 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I will keep things simple first. If I have 2 Lists of ints say ds1 and ds2 and in ds1 I put {1,1,2} and in ds2 I put {2} and then I use var except = ds1.Except(ds2) , except will contain only {1}. This I understand. However, if I make Lists of points say dsp1 and dsp 2 and in dsp1 I put {(1,2),(3,4)} and in dsp2 I put {(3,4),(5,6)} and then I use var except = dsp1.Except(dsp2) , except will contain all of them... i.e. {(1,2),(3,4),(5,6)} This behavior boggles my mind. Maybe I am missing something simple here. Either way when I use custom containers, i.e. List and my IEquatable object overrides GetHashCode and I have built my Equals properly, I am seeing behavior that resembles the opposite of what I posted for the integers. i.e. both '10' would get returned. Anyone have any idea??

    "9 Pregnent woman can not have a baby in 1 month" -Uknown

    L B 2 Replies Last reply
    0
    • L Lost User

      I will keep things simple first. If I have 2 Lists of ints say ds1 and ds2 and in ds1 I put {1,1,2} and in ds2 I put {2} and then I use var except = ds1.Except(ds2) , except will contain only {1}. This I understand. However, if I make Lists of points say dsp1 and dsp 2 and in dsp1 I put {(1,2),(3,4)} and in dsp2 I put {(3,4),(5,6)} and then I use var except = dsp1.Except(dsp2) , except will contain all of them... i.e. {(1,2),(3,4),(5,6)} This behavior boggles my mind. Maybe I am missing something simple here. Either way when I use custom containers, i.e. List and my IEquatable object overrides GetHashCode and I have built my Equals properly, I am seeing behavior that resembles the opposite of what I posted for the integers. i.e. both '10' would get returned. Anyone have any idea??

      "9 Pregnent woman can not have a baby in 1 month" -Uknown

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      More Info: Made a custom Point object "MyPoint" that inherits from IQuatable.. Here it is...

      class MyPoint : IEquatable
      {
          public int? X { get; set; }
          public int? Y { get; set; }
      
          #region IEquatable Members
      
          public bool Equals(MyPoint other)
          {
              // Check whether the compared object is null.
              if (Object.ReferenceEquals(other, null)) return false;
      
              // Check whether the compared object references the same data.
              if (Object.ReferenceEquals(this, other)) return true;
      
              bool match = false;
              bool compared = false;
      
              MyPoint oPoint = other as MyPoint;
      
              if (X.HasValue && oPoint.X.HasValue)
              {
                  compared = true;
                  match = X.Value.Equals(X.Value);
              }
      
              if (match && Y.HasValue && oPoint.Y.HasValue)
              {
                  compared = true;
                  match = Y.Value.Equals(oPoint.Y.Value);
              }
      
              return match && compared;
              
          }
      
          public override int GetHashCode()
          {
              int hash = 17;
              hash = X.HasValue ? (hash \* 23) + X.Value.GetHashCode() : (hash \* 23) + 1;
              hash = Y.HasValue ? (hash \* 23) + Y.Value.GetHashCode() : (hash \* 23) + 2;
              return hash;
          }
      
          #endregion
      }
      

      So in the dsp1 and dsp2 I changed them to be List and ran as set. This resulted in the following, {(1,2,(3,4),(3,4),(5,6)} Seriously?? :wtf: Modded- Has a typo in the Equals... It was changed to the above and now the same output is generated. i.e {(1,2,(3,4),(5,6)} So Why the heck am I getting (5,6) in this data set?

      "9 Pregnent woman can not have a baby in 1 month" -Uknown

      modified on Wednesday, September 30, 2009 4:08 PM

      G 1 Reply Last reply
      0
      • L Lost User

        More Info: Made a custom Point object "MyPoint" that inherits from IQuatable.. Here it is...

        class MyPoint : IEquatable
        {
            public int? X { get; set; }
            public int? Y { get; set; }
        
            #region IEquatable Members
        
            public bool Equals(MyPoint other)
            {
                // Check whether the compared object is null.
                if (Object.ReferenceEquals(other, null)) return false;
        
                // Check whether the compared object references the same data.
                if (Object.ReferenceEquals(this, other)) return true;
        
                bool match = false;
                bool compared = false;
        
                MyPoint oPoint = other as MyPoint;
        
                if (X.HasValue && oPoint.X.HasValue)
                {
                    compared = true;
                    match = X.Value.Equals(X.Value);
                }
        
                if (match && Y.HasValue && oPoint.Y.HasValue)
                {
                    compared = true;
                    match = Y.Value.Equals(oPoint.Y.Value);
                }
        
                return match && compared;
                
            }
        
            public override int GetHashCode()
            {
                int hash = 17;
                hash = X.HasValue ? (hash \* 23) + X.Value.GetHashCode() : (hash \* 23) + 1;
                hash = Y.HasValue ? (hash \* 23) + Y.Value.GetHashCode() : (hash \* 23) + 2;
                return hash;
            }
        
            #endregion
        }
        

        So in the dsp1 and dsp2 I changed them to be List and ran as set. This resulted in the following, {(1,2,(3,4),(3,4),(5,6)} Seriously?? :wtf: Modded- Has a typo in the Equals... It was changed to the above and now the same output is generated. i.e {(1,2,(3,4),(5,6)} So Why the heck am I getting (5,6) in this data set?

        "9 Pregnent woman can not have a baby in 1 month" -Uknown

        modified on Wednesday, September 30, 2009 4:08 PM

        G Offline
        G Offline
        Gideon Engelberth
        wrote on last edited by
        #3

        I am using your class and do not see the results you are getting (I am getting just [1,2]). Could you put up more code to show where you are declaring and filling ds1 and ds2. (Maybe even a complete little console app to show the output.)

        L 1 Reply Last reply
        0
        • G Gideon Engelberth

          I am using your class and do not see the results you are getting (I am getting just [1,2]). Could you put up more code to show where you are declaring and filling ds1 and ds2. (Maybe even a complete little console app to show the output.)

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Sure can.. Here it is:

          class Program
          {
          static List dsp1 = new List();
          static List dsp2 = new List();

              static void Main(string\[\] args)
              {
                  FillDataPoints1();
                  FillDataPoints2();
          
                  var except = dsp1.Except(dsp2);
          
                  StringBuilder sb = new StringBuilder();
                  sb.Append("{");
                  foreach (var item in except)
                  {
                      sb.Append("(" + item.X.ToString() + ", " + item.Y.ToString() + "), ");
                  }
                  string output = sb.ToString().Substring(0, sb.ToString().Length - 2) + "}";
                  Console.WriteLine(output);
                  Console.WriteLine("Press ANy Key To Continue...");
                  Console.Read();
              }
          
              static private void FillDataPoints1()
              {
                  dsp1.Add(new MyPoint()
                  {
                      X = 1,
                      Y = 2
                  });
          
                  dsp1.Add(new MyPoint()
                  {
                      X = 3,
                      Y = 4
                  });
              }
          
              static private void FillDataPoints2()
              {
                  dsp1.Add(new MyPoint()
                  {
                      X = 3,
                      Y = 4
                  });
          
                  dsp1.Add(new MyPoint()
                  {
                      X = 5,
                      Y = 6
                  });
              }
          }
          

          The MyPoint is that from above.

          "9 Pregnent woman can not have a baby in 1 month" -Uknown

          L 1 Reply Last reply
          0
          • L Lost User

            Sure can.. Here it is:

            class Program
            {
            static List dsp1 = new List();
            static List dsp2 = new List();

                static void Main(string\[\] args)
                {
                    FillDataPoints1();
                    FillDataPoints2();
            
                    var except = dsp1.Except(dsp2);
            
                    StringBuilder sb = new StringBuilder();
                    sb.Append("{");
                    foreach (var item in except)
                    {
                        sb.Append("(" + item.X.ToString() + ", " + item.Y.ToString() + "), ");
                    }
                    string output = sb.ToString().Substring(0, sb.ToString().Length - 2) + "}";
                    Console.WriteLine(output);
                    Console.WriteLine("Press ANy Key To Continue...");
                    Console.Read();
                }
            
                static private void FillDataPoints1()
                {
                    dsp1.Add(new MyPoint()
                    {
                        X = 1,
                        Y = 2
                    });
            
                    dsp1.Add(new MyPoint()
                    {
                        X = 3,
                        Y = 4
                    });
                }
            
                static private void FillDataPoints2()
                {
                    dsp1.Add(new MyPoint()
                    {
                        X = 3,
                        Y = 4
                    });
            
                    dsp1.Add(new MyPoint()
                    {
                        X = 5,
                        Y = 6
                    });
                }
            }
            

            The MyPoint is that from above.

            "9 Pregnent woman can not have a baby in 1 month" -Uknown

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Oops. My second one is not filled. THat is why my test code is not working... Ok well that works. Now I just need my more complicated one to work. Back to the drawing board. Thanks for your help though :)

            "9 Pregnent woman can not have a baby in 1 month" -Uknown

            1 Reply Last reply
            0
            • L Lost User

              I will keep things simple first. If I have 2 Lists of ints say ds1 and ds2 and in ds1 I put {1,1,2} and in ds2 I put {2} and then I use var except = ds1.Except(ds2) , except will contain only {1}. This I understand. However, if I make Lists of points say dsp1 and dsp 2 and in dsp1 I put {(1,2),(3,4)} and in dsp2 I put {(3,4),(5,6)} and then I use var except = dsp1.Except(dsp2) , except will contain all of them... i.e. {(1,2),(3,4),(5,6)} This behavior boggles my mind. Maybe I am missing something simple here. Either way when I use custom containers, i.e. List and my IEquatable object overrides GetHashCode and I have built my Equals properly, I am seeing behavior that resembles the opposite of what I posted for the integers. i.e. both '10' would get returned. Anyone have any idea??

              "9 Pregnent woman can not have a baby in 1 month" -Uknown

              B Offline
              B Offline
              bhogsett
              wrote on last edited by
              #6

              I am new to vb.net and LINQ. I do not understand what you understand when you said: "If I have 2 Lists of ints say ds1 and ds2 and in ds1 I put {1,1,2} and in ds2 I put {2} and then I use var except = ds1.Except(ds2) , except will contain only {1}. This I understand." I expected except to contain {1,1}. Please explain. What I am trying to do is this. Read a text file, exclude a group of words from the file and then use regex and linq to choose the words (maybe all words longer than 6 letters) count number of uses of a word and to group and order the output. Everything is working fine except the exclusion of words. I thought Except was the answer, but was very surprised to see that it removed duplicates from the target string. Any suggestions will be appreciated. Bill:confused:

              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