Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Tuesday, July 22, 2008

Using LINQ with ArcObjects

Are you using .NET 3.5 to do ArcObjects programming? Are you still manually enumerating through row or feature cursors? Listen up!

It's really very simple to get LINQ running with ArcObjects. All you have to do is add an extension method to either ICursor or IFeatureCursor like so:
public static class ICursorExtensions {

public static IEnumerable<IRow> AsEnumerable(this ICursor source) {
Func<IRow> next = () => source.NextRow();
IRow current = next();
while (current != null) {
yield return current;
current = next();
}
}
}


With your new extension method in hand, you can unleash LINQ on your ArcObjects code. You can now write code like this:
Int32 count = cursor.AsEnumerable().Count();


Or, you could write a query expression against two cursor objects:
var resultSeq =
from x in cursor1.AsEnumerable()
from y in cursor2.AsEnumerable()
where x.get_Value(0) = y.get_Value(0)
select new { Left = x, Right = y };


You can do a similar thing in F#:
type ICursor with
member c.AsEnumerable() =
seq {
let next = c.NextRow
let current = ref (next())
while !current <> null do
yield !current
do current := next()
}
let count = cursor.AsEnumerable() |> Seq.length

Sure beats manually manipulating an enumerator, doesn't it?