LINQ to ArcObjects

2974
1
09-12-2012 09:05 AM
MarkoApfel
New Contributor
Due to lack of time I decided to offer the code for all interested persons.

Linq2ArcObjects contains a LINQ-provider for feature cursors and sets. It allows you to use LINQ-style and lambda syntax for accessing features.

One simple LINQ sample to get all features with an area above a threshold of 3000:

var largeFeatures =
    from feature in features
    where (feature.GetValue("SHAPE_Area").ToDouble() > 3000)
    select feature;


The same with a lambda expression:

var largeFeatures =
    features.Where(feature => 
        (feature.GetValue("SHAPE_Area").ToDouble() > 3000));


This prototype has also some unit test to show you the usage of the constructs.

The whole documentation you could read in my blog.
And the source code is here.

Have fun ...
0 Kudos
1 Reply
CharlesShore
New Contributor
The following code worked fine for weeks, but now suddenly causes my ArcMap program to crash. 

The list zoningValues is supposed to contain a distinct, ordered list of zone codes.  The source of the codes is the field "ZO_ZONE" from an SDE feature class (zoningFClass).  In the while loop, zoningValues is first populated with the unsorted, non-distinct zoning codes.  The if statement contains the LINQ query to order and make distinct the list.  This part still works fine.

The final line is supposed to populate a combo box with the list values.  However, this line either makes my program crash, or starts a seemingly endless loop of constantly referring back to the query.  In the latter case, it's as if sortedList is a constructor object that needs to redefine itself each time.



        private List<string> zoningValues = new List<string>(300);

        internal void PopZoningList(IFeatureClass zoningFClass)
        {
            //Populate the Zoning values list
            IFeatureCursor searchCursor = zoningFClass.Search(null, true);
            int fieldNdx = searchCursor.FindField("ZO_ZONE");
            IFeature feature = null;
           
            //Loop through the Zoning feature class and populate a list with each ZO_ZONE value
            while ((feature = searchCursor.NextFeature()) != null)
            {
                zoningValues.Add(feature.get_Value(fieldNdx).ToString());
                feature = null;
            }
            //When the features are exhausted, sort the values and make them unique, then add them to the Zoning combo box
            if (feature == null && zoningValues.Count >= 2)
            {
            //sZones is an arbitrary variable name
                var sortedList = (from sZones in zoningValues
                select sZones)
                .Distinct().OrderBy(n => n);

                cboZone.DataSource = sortedList;[/SIZE]


Thanks in advance for looking into this.

Charlie Shore
0 Kudos