DEV Community

Mariem Moalla
Mariem Moalla

Posted on

Stop Looping: Use Dictionary for Fast Entity Access in .NET

In many .NET applications, you manage collections of entities, like users, orders, or products, and frequently need to look up items by their unique identifiers.

It’s tempting to just use a List or Collection, especially when you're adding items sequentially.

But here’s the issue: as your data grows, these lookups can quietly cause performance issues.

In this post, I’ll walk you through a common mistake I see in .NET codebases and explain why replacing this:

public Collection<EntityBase> Entities { get; init; }
Enter fullscreen mode Exit fullscreen mode

with this

private readonly IReadOnlyDictionary<Guid, EntityBase> entityLookup;
Enter fullscreen mode Exit fullscreen mode

can dramatically improve your application’s performance and clarity.


Let's say you have a list of 1,000 EntityBase instances:

var result = entities.FirstOrDefault(e => e.Id == requestedId);
Enter fullscreen mode Exit fullscreen mode

Seems harmless, right?

But here's the hidden issue:
Every call to FirstOrDefault() does a linear scan of the list.

If you're making this lookup often (in an API, a service), you're adding up O(n) costs repeatedly.

And many devs don't realize how often this occurs until performance problems appear in production.

And the more projects you have, the slower it gets.
Worse: if you're doing this lookup inside a loop or nested service calls, you're silently building a performance bottleneck.


The fix ?

Switch to a Dictionary for fast, constant-time lookups.
Instead of this:

public class MyService
{
    public List<EntityBase> Entities { get; } = new();

    public EntityBase? GetById(Guid id)
        => Entities.FirstOrDefault(e => e.Id == id);
}
Enter fullscreen mode Exit fullscreen mode

Do this:

public class MyService
{
    private readonly Dictionary<Guid, EntityBase> entityLookup;

    public MyService(IEnumerable<EntityBase> entities)
    {
        entityLookup = entities.ToDictionary(e => e.Id);
    }

    public EntityBase? GetById(Guid id)
        => entityLookup.TryGetValue(id, out var entity) ? entity : null;
}

Enter fullscreen mode Exit fullscreen mode

This is cleaner, faster, and shows intent more clearly.
Bonus: For extra safety, wrap your lookup in an IReadOnlyDictionary and initialize it once:

private readonly IReadOnlyDictionary<Guid, EntityBase> _entityLookup;
Enter fullscreen mode Exit fullscreen mode

Now when not to use a Dictionary ?
To be fair, Dictionary is not always the best tool. Avoid it when:

  • You care about order of insertion and need indexed access (list[0])
  • Your objects don’t have unique keys
  • You have a small collection and performance is irrelevant

Use the right tool for the right job.


📌 Follow me on LinkedIn for more .NET tips.

Top comments (0)