© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
14 replies
13eck

❔ Filter a ConcurrentDictionary

I have a
ConcurrentDictionary
ConcurrentDictionary
that I'm using for a small in-memory cache and I'm wondering what the best way to filter items out of it. one of the fields on the value stored in the dictionary is a
DateTimeOffset
DateTimeOffset
expiration time and I'm still new to using it.

Currently, I'm using
.Select()
.Select()
to filter out the expired items and casting it back to the Dict, overwriting the existing variable with a new one:

_cache = (ConcurrentDictionary<string, UserCacheItem>)_cache.Select(item => item.Value.Expiry > DateTimeOffset.Now);
_cache = (ConcurrentDictionary<string, UserCacheItem>)_cache.Select(item => item.Value.Expiry > DateTimeOffset.Now);


Is there a better way to do this?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Override ConcurrentDictionary EqualityComparer<TValue>?
C#CC# / help
3y ago
What do use in a multi-threaded environment?Dictionary with lock OR ConcurrentDictionary?
C#CC# / help
2y ago
✅ Would a ConcurrentDictionary be necessary if I am only adding and never removing?
C#CC# / help
3y ago
EFCore Optional filter / Composite filter
C#CC# / help
3y ago