Last active
May 4, 2017 16:42
-
-
Save stakx/5ed15119fc95647d836e to your computer and use it in GitHub Desktop.
Demonstrates how ArcObjects cursors can be transformed to `foreach`-able sequences.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// using ESRI.ArcGIS.Geodatabase; | |
// using System; | |
// using System.Collections.Generic; | |
// using System.Runtime.InteropServices; | |
public static IEnumerable<IRow> SearchAsEnumerable(this ITable table, IQueryFilter queryFilter, bool recycling) | |
{ | |
Func<ICursor> createCursor = () => table.Search(queryFilter, recycling); | |
return createCursor.AsEnumerable(); | |
} | |
private static IEnumerable<IRow> AsEnumerable(this Func<ICursor> createCursor) | |
{ | |
ICursor cursor = createCursor(); | |
try | |
{ | |
for (IRow row = cursor.NextRow(); row != null; row = cursor.NextRow()) | |
{ | |
yield return row; | |
} | |
} | |
finally | |
{ | |
Marshal.ReleaseComObject(cursor); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment