Using IEnumerator to iterate through a .NET Framework collection in Dynamics NAV

Kushal Pillay
Apr 19 · 2 min read

In C/AL, You can use the FOREACH statement to iterate through a .NET Framework collection or array object. It is the recommended approach, and provides a very simple construct for C/AL programmers:

FOREACH <Element> IN <List> DO  

The <List> variable must be a DotNet data type that is set to .NET Framework collection or array type.

In .NET, much of the repetitive constructs we use in our day to day lives as a programmer, like the Whlie Loop/For Loop etc are all implementations of the IEnumerator.

Today, I will show you how you can use the IEnumerator in NAV. Although it is always recommended to use the FOREACH statement, it is useful to know what goes on under the hood.

NavListEnumerator : System.Collections.Generic.IEnumerator`1.'mscorlib, Version=, Culture=neutral, PublicKeyToken=b77a5c561934e089'NavList: .NET Framework or Collection NavListItem: // Initialize .NET List to a NavList of Type <NavListItem>
NavList := GetListType();
NavListEnumerator := NavList.GetEnumerator();WHILE NavListEnumerator.MoveNext() DO BEGINNavListItem := NavListEnumerator.Current;MESSAGE(FORMAT(NavListItem.Amount));END;

I actually was in need of a way to iterate through a .NET Collection, and thought of this approach to solve the problem, later on, I found out that FOREACH already does this for us. Doesn't hurt to tinker around, always take the red pill ;)

Navigating 365

Thoughts, adventures and ideas of a Machine Learning, Dynamics 365 Developer

Kushal Pillay

Written by

Navigating 365

Thoughts, adventures and ideas of a Machine Learning, Dynamics 365 Developer

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade