Skip to content
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

Query: Adds LINQ Support for FirstOrDefault #4286

Merged
merged 10 commits into from
Jan 30, 2024

Conversation

adityasa
Copy link
Contributor

@adityasa adityasa commented Jan 27, 2024

Query: Adds LINQ Support for FirstOrDefault

Description

This change adds LINQ support for Enumerable.FirstOrDefault. At the moment only following overload is supported:
public static TSource? FirstOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source);

Specifically all of the following overloads are disallowed and suitable error is raised:
public static TSource FirstOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate, TSource defaultValue);
public static TSource FirstOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, TSource defaultValue);
public static TSource? FirstOrDefault<TSource> (this System.Collections.Generic.IEnumerable<TSource> source, Func<TSource,bool> predicate);

Since FirstOrDefault logic cannot entirely be expressed SQL query, it is achieved using a combination of SQL query and client side operation. Generated query in this case is of the form SELECT TOP 1 VALUE. CosmosQuery then executes a scalar FirstOrDefault operation on the results returned by the backend for such a query.

For e.g.
LINQ Expression:
getQuery(b) .Select(data => data.Flag) .Where(flag => flag) .FirstOrDefault()
SQL Query:
SELECT TOP 1 VALUE root ["Flag"] FROM root WHERE root ["Flag"]

Type of change

Please delete options that are not relevant.

  • [] Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • [] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [] This change requires a documentation update

Closing issues

To automatically close an issue: closes #IssueNumber

@adityasa adityasa force-pushed the users/adityasa/FirstOrDefault branch from 1dce38f to f70b19f Compare January 28, 2024 20:20
sboshra
sboshra previously approved these changes Jan 29, 2024
Copy link
Contributor

@sboshra sboshra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

@adityasa adityasa added the auto-merge Enables automation to merge PRs label Jan 29, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot merged commit 2c7c7ad into master Jan 30, 2024
20 checks passed
@microsoft-github-policy-service microsoft-github-policy-service bot deleted the users/adityasa/FirstOrDefault branch January 30, 2024 05:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
auto-merge Enables automation to merge PRs
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants