Skip to content

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 marked this pull request as ready for review January 27, 2024 23:12
@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
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.

4 participants