ZetCode

ASP.NET IEnumerable

last modified April 3, 2025

In this article, we explore the IEnumerable interface in ASP.NET 8. This interface is fundamental for working with collections in C# and .NET.

IEnumerable provides a simple way to iterate over collections. It's widely used in LINQ queries, foreach loops, and data binding scenarios in ASP.NET apps.

Basic Definition

IEnumerable is an interface in the System.Collections namespace. It exposes an enumerator that supports simple iteration over a collection of a specified type.

The interface contains a single method: GetEnumerator(). This method returns an IEnumerator object that can be used to iterate through the collection.

IEnumerable is the base interface for all non-generic collections. Its generic counterpart, IEnumerable<T>, is preferred in modern .NET applications.

Key features include deferred execution and lazy evaluation. Many LINQ methods return IEnumerable to enable query composition and efficient execution.

ASP.NET IEnumerable Example

The following example demonstrates IEnumerable usage in an ASP.NET Web API.

Program.cs
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();

var app = builder.Build();

app.MapControllers();
app.Run();

This sets up a basic ASP.NET application with controller support. The MapControllers method enables attribute routing for controllers.

Controllers/BooksController.cs
using Microsoft.AspNetCore.Mvc;
using System.Collections;

[ApiController]
[Route("api/[controller]")]
public class BooksController : ControllerBase
{
    private static List<Book> _books = new()
    {
        new Book(1, "The Hobbit", "J.R.R. Tolkien"),
        new Book(2, "Dune", "Frank Herbert"),
        new Book(3, "Neuromancer", "William Gibson")
    };

    [HttpGet]
    public IEnumerable<Book> GetAllBooks()
    {
        return _books;
    }

    [HttpGet("filter")]
    public IEnumerable<Book> FilterBooks([FromQuery] string term)
    {
        return _books.Where(b => 
            b.Title.Contains(term, StringComparison.OrdinalIgnoreCase) ||
            b.Author.Contains(term, StringComparison.OrdinalIgnoreCase));
    }

    [HttpGet("ids")]
    public IEnumerable<int> GetAllBookIds()
    {
        return _books.Select(b => b.Id);
    }

    [HttpGet("non-generic")]
    public IEnumerable GetNonGenericBooks()
    {
        return _books;
    }
}

public record Book(int Id, string Title, string Author);

This controller demonstrates four different IEnumerable scenarios. The first method returns all books as IEnumerable<Book> when hitting /api/books.

The second method filters books by title or author using LINQ's Where method. The third method projects just the book IDs using Select, returning IEnumerable<int>.

The fourth method shows non-generic IEnumerable usage. While functional, the generic version provides better type safety and is preferred in modern code.

IEnumerable is ideal for API return types because it supports deferred execution. The actual enumeration happens when the response is being serialized.

Source

Microsoft IEnumerable Documentation

In this article, we have explored the IEnumerable interface in ASP.NET 8. This fundamental interface enables efficient collection processing in .NET apps.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all ASP.NET tutorials.