You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This method is causing me some trouble when I'm using it inside a query (CQRS-wise) when source is an IQueryable<T>.
public static Maybe<T> TryFirst<T>(this IEnumerable<T> source)
{
source = source as ICollection<T> ?? source.ToList();
if (source.Any()) return Maybe<T>.From(source.First());
return Maybe<T>.None;
}
Since IQueryable<T> is not an ICollection<T> the call to .ToList() causes the whole database table to be fetched instead of only the first element (this is an issue when the table is really big). I understand the goal of calling ToList() is to optimize the subsequent calls to Any() and First() but at the cost of a nasty performance side-effect.
As far as I understand, this method could even be removed (or marked deprecated) because it is doing nothing more than what is already possible by calling IEnumerable<T>.FirstOrDefault() coupled with the implicit cast operator from T to Maybe<T> ?
The text was updated successfully, but these errors were encountered:
Note that the fix here is to use another extension method that works on top of IQueriable, like the one @jeffward01 suggested. Any method working on top of IEnumerable (with any implementation) will prompt EF Core to load the full data set into memory.
This method is causing me some trouble when I'm using it inside a query (CQRS-wise) when source is an
IQueryable<T>
.Since
IQueryable<T>
is not anICollection<T>
the call to.ToList()
causes the whole database table to be fetched instead of only the first element (this is an issue when the table is really big). I understand the goal of callingToList()
is to optimize the subsequent calls toAny()
andFirst()
but at the cost of a nasty performance side-effect.As far as I understand, this method could even be removed (or marked deprecated) because it is doing nothing more than what is already possible by calling
IEnumerable<T>.FirstOrDefault()
coupled with the implicit cast operator fromT
toMaybe<T>
?The text was updated successfully, but these errors were encountered: