none
LINQ query seems to feed back on itself RRS feed

  • Question

  • Hi Alan,

    Maybe you can figure out what's up with my code.  It worked fine for weeks, but now suddenly causes my ArcMap program to crash.  I suspect that you're
    not that familiar with ArcGIS, but perhaps you could help out on the LINQ part.

    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 ArcGIS 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)
                {
                    var sortedList = (from sZones in zoningValues
                    select sZones)
                    .Distinct().OrderBy(n => n);

                    cboZone.DataSource = sortedList;

    Thanks in advance for looking into this.

    Charlie Shore

    Friday, October 26, 2012 7:26 PM

Answers

  • Hi Charlie,

    Welcome to the MSDN forum.

    Please try to use ToList() method in the end this line like:

    var sortedList = (from sZones in zoningValues
                     select sZones)
                     .Distinct().OrderBy(n => n).ToList();
     
    cboZone.DataSource = sortedList;
    

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Alexander Sun Monday, November 5, 2012 9:29 AM
    Monday, October 29, 2012 7:17 AM

All replies

  • Hi Alan,

    Maybe you can figure out what's up with my code.  It worked fine for weeks, but now suddenly causes my ArcMap program to crash.  I suspect that you're
    not that familiar with ArcGIS, but perhaps you could help out on the LINQ part.

    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 ArcGIS 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)
                {
                    var sortedList = (from sZones in zoningValues
                    select sZones)
                    .Distinct().OrderBy(n => n);

                    cboZone.DataSource = sortedList;

    Thanks in advance for looking into this.

    Charlie Shore

    • Merged by Alexander Sun Monday, October 29, 2012 6:55 AM Duplicate
    Friday, October 26, 2012 8:36 PM
  • Hi Charlie,

    Welcome to the MSDN forum.

    Please try to use ToList() method in the end this line like:

    var sortedList = (from sZones in zoningValues
                     select sZones)
                     .Distinct().OrderBy(n => n).ToList();
     
    cboZone.DataSource = sortedList;
    

    Have a nice day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Alexander Sun Monday, November 5, 2012 9:29 AM
    Monday, October 29, 2012 7:17 AM