Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ef Core: ToHashSetAsync()

I have noticed that Microsoft.EntityFrameworkCore Linq extension methods dose not contain ToHashSet Async version, I'm assuming the right & safe way to materialize the object is HashSet since it protects against duplication, did I missed something? if it's so important why there is no Async version of it?

like image 365
Alphas Supremum Avatar asked Apr 29 '26 18:04

Alphas Supremum


2 Answers

In code, your tables are represented as a DbSet which is an IListSource.

IListSource has one method:

IList GetList();

So everything that's returned from EntityFrameworkCore is an IList; an indexable, non-unique bag of values. Whereas a HashSet, being much faster to access, is a unique, non-indexed bag of values. Equality Comparison determines its uniqueness, and anything more than a few columns starts to get really expensive to calculate.

Whilst you might think it'd be more performant to go straight to a HashSet, if that's what you need, the data is already a List. ToListAsync() just calls a rose a rose, and loads the data. Then you can HashSet it if needed.

like image 123
Tod Avatar answered May 01 '26 07:05

Tod


As of EFCore 9 there is a .ToHashSetAsync() methods in case you need to lookup the results many times. For example: you select some id column from DB and want to filter something that is already in you RAM based on these ids.

Yes, in-memory operations in most cases are blazingly fast compared to database roundtrips, but untidy use of O(N) searches in basic lists may slow down even memory operations.

Sources is very much the same as .ToListAsync() (not as in .ToArrayAsync() with List->Array conversion)

Docs

like image 33
ornic Avatar answered May 01 '26 07:05

ornic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!