Skip to content

Instantly share code, notes, and snippets.

@stakx
Last active May 4, 2017 16:42
Show Gist options
  • Save stakx/5ed15119fc95647d836e to your computer and use it in GitHub Desktop.
Save stakx/5ed15119fc95647d836e to your computer and use it in GitHub Desktop.
Demonstrates how ArcObjects cursors can be transformed to `foreach`-able sequences.
// 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