Except Confusion
-
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 usevar 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
-
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 usevar 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
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
-
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
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.)
-
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.)
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
-
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
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
-
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 usevar 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
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: