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

Support LINQ querying for records #3728

Open
onionhammer opened this issue Feb 24, 2023 · 2 comments
Open

Support LINQ querying for records #3728

onionhammer opened this issue Feb 24, 2023 · 2 comments
Assignees
Labels
feature-request New feature or request LINQ QUERY
Milestone

Comments

@onionhammer
Copy link
Contributor

onionhammer commented Feb 24, 2023

Is your feature request related to a problem? Please describe.
Currently when invoking Select() for records, an exception is thrown

"Constructor invocation is not supported., Windows/10.0.19045 cosmos-netstandard-sdk/3.29.4"

Describe the solution you'd like
Support constructors to select the members specified in constructors and let the deserializer handle the rest.

Describe alternatives you've considered
There are no alternatives except to not use records or not use LINQ.

Additional context
Example:

// record
record UserRecord(string Id)
{
    public string? Name { get; set; }
}

// query:
query.Select(x => new UserRecord(x.id, x.Name));

could result in

SELECT c.id as Id, c.Name
FROM c

which yields

[
   { "Id": "12345", "Name": "Joe" }
]

and skips potentially large JSON properties

@adityasa
Copy link
Contributor

No committed plans to address this as of now.

@adityasa adityasa added this to the Backlog milestone May 29, 2023
@Maya-Painter Maya-Painter added the feature-request New feature or request label Feb 8, 2024
@bartelink
Copy link
Contributor

bartelink commented Mar 24, 2024

There are no alternatives except to not use records or not use LINQ.

@onionhammer in your specific case, you should be able to do query.Select(x => new { id = x.id; x.Name}) and then map that to your record (after you materialize the result against that anonymous type though?


In case this does ever get looked at... I'm trying to do the moral equivalent of this in F#, which is to do query.Select(x => {| id = id = x.id; Name = x.Name |}). This maps to a similar anonymous record type as C# anonymous type expressions. Sadly it currently yields me Microsoft.Azure.Cosmos.Linq.DocumentQueryException: Constructor invocation is not supported

I have a disgusting workaround: https://stackoverflow.com/a/78206722/11635, but ideally the LINQ support could desugar the object creation into an equivalent SELECT as the C# anonymous type expression does.

Basically my ask is the same as the OP: object construction expressions like this should map to JSON projections, and then it should be down to the caller to ensure that the JSON object that comes back can be deserialized to the Result type of the Expression

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request New feature or request LINQ QUERY
Projects
None yet
Development

No branches or pull requests

6 participants