Get Distinct Value From Shape File Column

We have some feature classes or shape files and we want to get distinct values from a particular column and its count. This is a common task which we need when we are working with feature classes/Shape files. Suppose we have a feature class or shape file and its name is "Landuse" and in Landuse feature class we have attribute "FEAT_TYPE". We want to get the distinct value from "FEAT_TYPE" column and also find the count of each "FEAT_TYPE". To get the distinct values of a particular column from shape file or feature class is very time consuming if we try to find a distinct value using loop. Because using loop it will take time and it will hurt the performance of the application also. To solve this problem we can use data statistics interface of ArcObject to find the distinct value and its count as well.This technique requires fewer lines of code and performance is also very good.

To implement this approach without using loop first we get the object of workspacefactory. Once we get the object of workspacefactory after that using method OpenfromFile() we get the object of workspace. Once we get the object of workspace we can cast this workspace object to ifeatureworkspace and use method Openfeatureclass to get object of featureclass. Use the Search method of featureclass we will get icursor and then we are ready to use IDatastatistics to get desired result.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Text;  
  7. using System.Windows.Forms;  
  8. using ESRI.ArcGIS.DataSourcesFile;  
  9. using ESRI.ArcGIS.DataSourcesGDB;  
  10. using ESRI.ArcGIS.Geodatabase;  
  11. using ESRI.ArcGIS.esriSystem;  
  12. using ESRI.ArcGIS.Geometry;  
  13. using System.Collections;  
  14. namespace WindowsApp {  
  15.     publicpartialclassForm1: Form {  
  16.         public Form1() {  
  17.             InitializeComponent();  
  18.         }  
  19.         privatevoid button1_Click(object sender, EventArgs e) {  
  20.             IWorkspaceFactory pwsf = newShapefileWorkspaceFactory();  
  21.             IWorkspace pws = pwsf.OpenFromFile(@ "D:\YS\ShapeFiles", 0);  
  22.             IFeatureWorkspace pfeatWs = (IFeatureWorkspace) pws;  
  23.             IFeatureClass pfc = pfeatWs.OpenFeatureClass("LandUseA.shp");  
  24.             IFeatureCursor pfeatCur = pfc.Search(nullfalse);  
  25.             IDataStatistics pDataStat = newDataStatisticsClass();  
  26.             pDataStat.Cursor = (ICursor) pfeatCur;  
  27.             pDataStat.Field = "FEAT_TYPE";  
  28.             IEnumerator pEnum = pDataStat.UniqueValues;  
  29.             Boolean chk = pEnum.MoveNext();  
  30.             List < string > distinctValues = newList < string > ();  
  31.             while (chk) {  
  32.                 string Name = (String) pEnum.Current;  
  33.                 distinctValues.Add(Name);  
  34.                 chk = pEnum.MoveNext();  
  35.             }  
  36.         }  
  37.     }  
  38. }  
Next Recommended Reading Read Shape Files Using ArcObject