I have a street layer and I only need to export a subset of features to a new shapefile where the "TestDone" attribute field is not '0' (Zero).

My current code (which is pasted below) exports the total street layer.

I can use a query filter to get the non zero values out and assign it to an ICursor. But then not sure how to convert it to a IfeatureClass. (Not really sure whether the ICursor and then trying to convert to IFeatureClass is the correct way to go even).

How can I just get features that have a non zero value in the "TestDone" attribute field and then convert that to a feature layer as the second argument in the ExportFeatureClass method (please see below code) needs a IFeatureClass.

Any help with some code guidance will be greatly appreciated.

*****CODE *******

private void btnExport_Click(object sender, EventArgs e)
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Shape file|*.shp";
saveFileDialog1.DefaultExt = "shp";
saveFileDialog1.AddExtension = true;
saveFileDialog1.InitialDirectory = @"C:/";
saveFileDialog1.Title = "Check data";
if (saveFileDialog1.FileName != "")
string strFile = System .IO.Path.GetFileName ( saveFileDialog1.FileName) ;
IMap pMap = default(IMap);
IWorkspaceFactory pWorkspaceFactory = default(IWorkspaceFactory);
IWorkspace pWorkspace = default(IWorkspace);
IFileName pFileName = default(IFileName);
IFeatureLayer2 pFeatureLayer = default(IFeatureLayer2);
IFeatureClass pFeatureClass = default(IFeatureClass);
IPropertySet pPropertySet = default(IPropertySet);
IGeometryDef pGeometryDef = default(IGeometryDef);
IGeometryDefEdit pGeometryDefEdit = default(IGeometryDefEdit);
string deletefile = null;

pMap = clsFunctions.pMxDocument.FocusMap;
pFileName = new FileNameClass();
pFileName.Path = System .IO.Path.GetDirectoryName (saveFileDialog1.FileName);
pPropertySet = new PropertySet();
pPropertySet.SetProperty("DATABASE", pFileName.Path);
pWorkspaceFactory = new ESRI.ArcGIS.DataSourcesFile.ShapefileWorkspaceFact ory();
pWorkspace = pWorkspaceFactory.Open(pPropertySet, 0);

IGeographicCoordinateSystem pGCS = default(IGeographicCoordinateSystem);
SpatialReferenceEnvironment pSpatialReferenceEnv = default(SpatialReferenceEnvironment);
ISpatialReference2 pSpatialReference = default(ISpatialReference2);
pSpatialReferenceEnv = new SpatialReferenceEnvironment();
pGCS = pSpatialReferenceEnv.CreateGeographicCoordinateSys tem(Convert.ToInt16(esriSRGeoCSType.esriSRGeoCS_WG S1984));
pSpatialReference = (ISpatialReference2)pGCS;

ILayer pLayer;
int intI = 0;

    for (intI = 0; intI <= pMap.LayerCount - 1; intI++)
    pLayer = pMap.get_Layer(intI);
    if (pLayer.Name == cmbStreets.Text)
    pFeatureLayer = (IFeatureLayer2)pLayer;

pFeatureClass = pFeatureLayer.FeatureClass;
pGeometryDef = new GeometryDef();
pGeometryDefEdit = (IGeometryDefEdit)pGeometryDef;
pGeometryDefEdit.GeometryType_2 = pFeatureClass.ShapeType;
pGeometryDefEdit.SpatialReference_2 = pSpatialReference;

ExportFeatureClass(strFile, pFeatureClass, pWorkspace, pSpatialReference, pGeometryDef);

public void ExportFeatureClass(string strLayerName, IFeatureClass pFeatureClass, IWorkspace pOutWorkspace, ISpatialReference pSpatialReference, IGeometryDef pGeometryDef)
IFeatureClassName pInFeatureClassName = default(IFeatureClassName);
IDataset pDataset = default(IDataset);
IWorkspaceName pOutWorkspaceName = default(IWorkspaceName);
IFeatureClassName pOutFeatureClassName = default(IFeatureClassName);
IDatasetName pDatasetName = default(IDatasetName);
IExportOperation pExportOperation = default(IExportOperation);
IWorkspace pInWorkspace = default(IWorkspace);

pDataset = (IDataset)pFeatureClass;
pInFeatureClassName = (IFeatureClassName)pDataset.FullName;
pInWorkspace = pDataset.Workspace;
pDataset = (IDataset)pOutWorkspace;
pOutWorkspaceName = (IWorkspaceName)pDataset.FullName;

pOutFeatureClassName = new FeatureClassNameClass();
pDatasetName = (IDatasetName)pOutFeatureClassName;
pDatasetName.Name = strLayerName;
pDatasetName.WorkspaceName = pOutWorkspaceName;

pExportOperation = new ExportOperation();
pExportOperation.ExportFeatureClass((IDatasetName) pInFeatureClassName, null, null, pGeometryDef, pOutFeatureClassName, 0);

pInFeatureClassName = null;
pDataset = null;
pOutWorkspaceName = null;
pOutFeatureClassName = null;
pDatasetName = null;
pExportOperation = null;
pInWorkspace = null;
  • 1
    You should reformulate this to a question. Dec 17, 2010 at 9:16
  • 1
    What is the deal with default() all over the place? default() is a C# operator introduced in C# 2.0 and it returns null for reference types.
    – Petr Krebs
    Dec 17, 2010 at 11:16

2 Answers 2


You are using the IExportOperation interface, it allows you to specify the query filter when exporting data so that you do not need to deal with cursors yourself.

You can modify your code by first creating the query filter:

var queryFilter = new QueryFilterClass();
queryFilter.SubFields = "*";
queryFilter.WhereClause = "NOT TestDone=0";

Then adding the query filter as a parameter to your export function and using it in your IExportOperation.ExportFeatureClass call:

public void ExportFeatureClass(string strLayerName, IFeatureClass pFeatureClass, IWorkspace pOutWorkspace, ISpatialReference pSpatialReference, IGeometryDef pGeometryDef, IQueryFilter pQueryFilter)
    pExportOperation.ExportFeatureClass((IDatasetName)pInFeatureClassName, pQueryFilter, null, pGeometryDef, pOutFeatureClassName, 0);
  • You saved my day. Just what the doctor ordered. Works great. Thanks a lot
    – user1473
    Dec 17, 2010 at 20:17

Well something like this should do it:

Dim featureLayer As IFeatureLayer = CType(layer, IFeatureLayer)
Dim queryFilter As IQueryFilter = New QueryFilter()
queryFilter.SubFields = "*"
queryFilter.WhereClause = "TestDone<>0"

Dim featCur As IFeatureCursor = featureLayer.Search(queryFilter, False)

See the SQL reference

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.