-
Notifications
You must be signed in to change notification settings - Fork 309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
IQueryable - TryFirst (async) -- Replacement for .FirstOrDefaultAsync() -- Is this helpful for a PR? #473
Comments
Perhaps this would be better in a new nuget extension package called:
The thing is:
I understand why you went the |
The comments on this SO post have some good information about the benefits of Comment Question:
Comment Reply:
|
Lucian Wischik, who worked on the compiler for async/await pattern is clear:
So everyting you say regarding having API with BTW public static async Task<Maybe<T>> TryFirstAsync<T>(
this IQueryable<T> source,
Expression<Func<T, bool>> predicate,
CancellationToken cancellationToken = default)
{
var firstOrNull = await source.FirstOrDefaultAsync(predicate, cancellationToken);
if (firstOrNull == null)
{
return Maybe<T>.None;
}
return Maybe<T>.From(firstOrNull);
}
// Usage : IQuery<string> query = XXX;
// var item = await query.TryFirstAsync(x => x == "hello"); |
We can add these 2 extension methods if we don't have them yet. Regarding your second overload: please use @maxime-poulain 's version. This code: var firstOrEmpty = source.AsEnumerable()
.Where(predicate)
.Take(1)
.AsQueryable(); doesn't work because converting the |
I was looking at opening a PR with recent proposal/request in the issue part and While I assume @vkhorikov (to him to confirm or not) doesn't want to bother to maintain a Depending on the answer, you can either close the ticket or create that NuGet :). |
Ah, indeed. I forgot it'd require a new dependency. We definitely don't want the library to depend on EF Core. |
Perhaps we can group this into the set of 'new projects' PR that will split |
Maybe. But that's something someone else would need to do, I don't have much time nowadays, unfortunately. |
Hello all,
I am new to this library, I am working with EFCore. In my scenerio I am working with
IQueryable<T>
.I wanted a method like
.FirstOrDefaultAsync()
, because when I used.FirstOrDefaultAsync()
, my return object was a pain, and looked like:Result<Maybe<TEntity?>, Error>
, the nullable?
was giving me warnings in code.I saw the methods
TryFirst
andTryLast
- success I thought! But then I realized that these are not async, and do not useIQueryable
I created these methods for
async
andIQueryable
:Note
Note: I realize that we do not use the
Async
suffix, however, using the suffix made things must easier, because without the suffix, the method would default to theIEnumerable<T>
version, when I wanted to use theIQueryable<T>
version.Question
Is this helpful at all?
What should I do to replace
IQueryable<T>.FirstOrDefaultAsync()
in the future if this code is not helpful?The text was updated successfully, but these errors were encountered: