ObjectResult<T>.GetEnumerator Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns an enumerator that iterates through the query results.
public:
virtual System::Collections::Generic::IEnumerator<T> ^ GetEnumerator();
public System.Collections.Generic.IEnumerator<T> GetEnumerator ();
abstract member GetEnumerator : unit -> System.Collections.Generic.IEnumerator<'T>
override this.GetEnumerator : unit -> System.Collections.Generic.IEnumerator<'T>
Public Function GetEnumerator () As IEnumerator(Of T)
Returns
An enumerator that iterates through the query results.
Implements
Examples
This example returns an ObjectResult<T> from the Execute method. Then it gets an enumerator and iterates through the query results. At the end, it releases the enumerator and the ObjectResult<T> object.
using (AdventureWorksEntities context =
new AdventureWorksEntities())
{
ObjectSet<Product> query = context.Products;
ObjectResult<Product> queryResults = null;
System.Collections.IEnumerator enumerator = null;
try
{
queryResults = query.Execute(MergeOption.AppendOnly);
// Get the enumerator.
enumerator = ((System.Collections.IEnumerable)queryResults).GetEnumerator();
// Iterate through the query results.
while (enumerator.MoveNext())
{
Product product = (Product)enumerator.Current;
Console.WriteLine("{0}", product.Name);
}
// Dispose the enumerator
((IDisposable)enumerator).Dispose();
}
finally
{
// Dispose the query results and the enumerator.
if (queryResults != null)
{
queryResults.Dispose();
}
if (enumerator != null)
{
((IDisposable)enumerator).Dispose();
}
}
}
Remarks
The enumerator must be disposed when no longer needed. Using the foreach
statement (For Each
in Visual Basic) ensures that the enumerator is correctly disposed when iteration over the results is complete.
Applies to
See also
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for