[GIS] Comparing two featureclasses using C#

arcobjectsc

I'm sorry, I know this is a pretty simple question but am having trouble getting this to work. Basically, I have 2 linear featureclasses and they both have the same fields and spatial reference. FC1 has 675 records and FC2 only has 505 records. So how can I check what Prop_ID(s) are missing from FC2? I have some code that will copy the missing record from FC1 to FC2 but must first determine the missing Prop_ID.

many thanks

Best Answer

Since you tagged this question with arcobjects and c#, and since not all geodatabases support the SQL IN with a subquery, the following untested code shows how you could do this with System.Collections.Hashtable.

private List<int> Diff(ITable table1, ITable table2, string idfldName)
{
    // return the oid of each row in table1 whose id doesn't appear in table2
    var outList = new List<int>();

    var ht1 = HashRows(table1, idfldName);
    var ht2 = HashRows(table2, idfldName);
    foreach (object key in ht1.Keys)
    {
        if (!ht2.ContainsKey(key))
            outList.Add((int)ht1[key]);
    }
    return outList;
}

private Hashtable HashRows(ITable table, string idField)
{
    Hashtable ht = new Hashtable();
    ICursor cur = table.Search(null, true);
    try
    {
        int idx = table.FindField(idField);
        if (idx == -1)
            throw new Exception("Field not found: " + idField);
        IRow row;
        while ((row = cur.NextRow()) != null)
        {
            if(!ht.ContainsKey(row.get_Value(idx)))
                ht.Add(row.get_Value(idx),row.OID);
            else
                throw new Exception("duplicate key encountered at OID = " + row.OID);
        }
    }
    catch
    {
        throw;
    }
    finally
    {
        System.Runtime.InteropServices.Marshal.FinalReleaseComObject(cur);
    }
    return ht;
}
Related Question