fsharp / fsharp/fslang-suggestions

Inconsistent anonymous record instantiation on `IQueryable`

Open
#1,249 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
No language data
Stars
373
Forks
21
PR merge metrics
No merged PRs in 30d

Description

When projecting an anonymous record on `IQueryable` the generated tree contains a `Delegate.Invoke` for the anonymous type constructor, this causes some parsers/drivers to be unable to understand the operation (eg. `Entity Framework`)

This looks to happen when accessing deep members and having long record member names:

```fsharp
open System
open System.Linq

type Person = { Name: string; Id : int }
type Wrapper = { Person: Person }

let data = [
{ Person = { Name = "One"; Id = 1 } }
{ Person = { Name = "Two"; Id = 2 } }
{ Person = { Name = "Three"; Id = 3 } }
]

let queryWithInvoke =
data
.AsQueryable()
.Select(fun x -> {| Other = {| Name = x.Person.Name; Id = x.Person.Id |} |})

// short label names do not generate an invoke call
let queryWithoutInvoke =
data
.AsQueryable()
.Select(fun x -> {| Other = {| A = x.Person.Name; B = x.Person.Id |} |})

printfn "%A" queryWithInvoke.Expression;
printfn "------"
printfn "%A" queryWithoutInvoke.Expression
```

Outputs:
```csharp
[{ Person = { Name = "One"
Id = 9a093c1d-9a3c-4366-806e-7c32b4388a1d }
Value = 0 }; { Person = { Name = "Two"
Id = ded2fd37-69cd-4d56-bc05-69e539264301 }
Value = 1 }; { Person = { Name = "Three"
Id = 798c98fb-a772-4911-84cc-63101b369cb9 }
Value = 2 }].Select(x => new <>f__AnonymousType1021199106`1(Name => new <>f__AnonymousType3987781292`2(x.Person.Id, Name).Invoke(x.Person.Name)))
------
[{ Person = { Name = "One"
Id = 9a093c1d-9a3c-4366-806e-7c32b4388a1d }
Value = 0 }; { Person = { Name = "Two"
Id = ded2fd37-69cd-4d56-bc05-69e539264301 }
Value = 1 }; { Person = { Name = "Three"
Id = 798c98fb-a772-4911-84cc-63101b369cb9 }
Value = 2 }].Select(x => new <>f__AnonymousType1021199106`1(new <>f__AnonymousType2589797714`2(x.Person.Name, x.Person.Id)))
```
The problem is:
```csharp
new <>f__AnonymousType1021199106`1(
Name => new <>f__AnonymousType3987781292`2(
x.Person.Id, Name
)
.Invoke(x.Person.Name)) // <- here
```

The first case generates a **plain constructor**, and the second injects an **IIFE** for `Person.Name` which causes inconsistency query parsing

## Pros and Cons

The advantages of making this adjustment to F# are:
Consistent between generated IQueryables

The disadvantages of making this adjustment to F# are: N/A

## Extra information

Estimated cost (XS, S, M, L, XL, XXL): ?

## Affidavit (please submit!)

Please tick this by placing a cross in the box:
* [x] This is not a question (e.g. like one you might ask on [stackoverflow](http://stackoverflow.com)) and I have searched stackoverflow for discussions of this issue
* [x] I have [searched both open and closed suggestions on this site](http://github.com/fsharp/fslang-suggestions/issues) and believe this is not a duplicate
* [x] This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:
* [x] This is not a breaking change to the F# language design
* [ ] I or my company would be willing to help implement and/or test this

## For Readers

If you would like to see this issue implemented, please click the :+1: emoji on this issue. These counts are used to generally order the suggestions by engagement.

Contributor guide

No contributing guide indexed for this repository

Research direction

No source file or test is named. Start by running the supplied F# IQueryable example and comparing the two generated expression trees, then trace the compiler path responsible for anonymous record construction. Done means equivalent projections no longer differ through a Delegate.Invoke/IIFE shape that query parsers cannot understand.

Written by the indexing model from the issue text.

Assessment

Tech stack
fsharp
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.