-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Allow for specify return value on System.Linq.Enumerable.*OrDefault methods #48886
Conversation
Tagging subscribers to this area: @eiriktsarpalis |
Are the errors here code issues or an issue with changes being synced? I haven't contributed since the dotnet runtime was split across several repositories so I'm not familiar with all that's going on behind the scenes. Edit: Didn't push ref assemblies. This has been resolved. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add unit tests for the new methods as well. Thanks.
Hi @Foxtrek64, have you been able to take a look at PR feedback? Thanks! |
Yes. Sorry, I've had a busy few days. I'll implement these changes tomorrow and have them ready for review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add similar unit tests for the overloads accepting predicates.
approved accidentally, PR still has pending feedback
src/libraries/System.Linq.Queryable/tests/FirstOrDefaultTests.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Other than some minor changes, LGTM.
Thank you for your contribution!
Thanks @Foxtrek64. Note that there are a few failing unit tests related to these changes. Could you take a look? |
I did notice what I think was a logic error on my part and patched it. Hopefully it resolves the test issues, or at least some of them. I tried testing on my ends but I get strange results, like Edit: I've fixed this issue but now I'm running into issues with the Queryable side of things. After hashing this out in the .Net Foundation Discord, we believe this to be an issue with the BCL itself in that the overload I'm attempting to implement does not exist there. I'm not entirely sure doing something like this is even possible given that it would require splitting things into multiple queries on the BCL side. I can "fix" it by doing something like this: [DynamicDependency("FirstOrDefault`1", typeof(Enumerable))]
public static TSource FirstOrDefault<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, TSource defaultValue)
{
if (source == null)
throw Error.ArgumentNull(nameof(source));
if (predicate == null)
throw Error.ArgumentNull(nameof(predicate));
using var result = source.Where(predicate).Take(1).GetEnumerator();
if (result.MoveNext())
return result.Current;
return defaultValue;
// Skip this stuff
/*
return source.Provider.Execute<TSource>(
Expression.Call(
null,
CachedReflectionInfo.FirstOrDefault_TSource_4(typeof(TSource)),
source.Expression, Expression.Quote(predicate), Expression.Constant(defaultValue, typeof(TSource))
));
*/
} But this is less than ideal for the obvious reason that it cannot be used in the inner part of a query, like this: var query = context.SomeTable.Select(t => t.SubTable.Select(st => st.Value).FirstOrDefault(v => v > 0, -1)); @eiriktsarpalis @stephentoub How would you recommend going about this? It may be best to only update Alternatively, if you two or anyone else has any ideas, I'm all ears. It may be possible to implement this in an appropriate manner, but I'm not entirely certain what that might look like. |
Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
@Foxtrek64 I rebased your branch and added fixes for the failing tests. |
Seems a mix of me just being blind and missing a couple things and some random number being wrong somewhere that I would have never thought to look at. Glad we got that sorted. |
Thanks @Foxtrek64! |
Fixes #20064