This project has moved and is read-only. For the latest updates, please go here.

Schema.Fields.Contains not working?

Oct 15, 2012 at 4:04 PM

I am adding SimpleField items to a schema, and trying to not add any duplicates.  I am trying to use the Contains function to prevent duplicates.  Here is how I am using it:

SimpleField sf = new SimpleField();
sf.FieldType = ai.DataType.ToString();
sf.Name = ai.DBFName;

if (schema.Fields.ToList().Contains(sf) == false)

Is this the correct way to use it?  I also tried "if (schema.Fields.Contains(sf) == false)", but I got the same result (duplicate fields in my schema).

Oct 15, 2012 at 7:39 PM

The Contains method you're using is from LINQ so will try to use the objects Equals method.

Since SimpleField doesn't override this method then reference equality will be used. You can either modify the source of SimpleField to implement the IEquatable interface or create a class in your own assembly that implements the IEqualityComparer interface and pass that into one of the overloads of Contains.

Hope it helps - let me know if you get stuck.