Skip to content

Conversation

@MichalMarsalek
Copy link

@MichalMarsalek MichalMarsalek commented Jul 17, 2025

Pull Request Template

Description

This PR fixes a bug described in #1664 where captured variables in LINQ expressions would not get expanded leading to a {"testName": "Test"}["testName"] expression (instead of the expected "Test") in the translated SQL, leading to an error reported by Cosmos.

However the problem appears to be more general as the issue seems to be that the recursion that partially evaluates expressions (so among other things resolves the captured variables and therefore effectively changes {"testName": "Test"}["testName"] to "Test") terminates whenever it encounters a node of type MemberInitExpression. This explains the difference in behaviour between anonymous types and a class as described in #1664, as anonymous types do not contain the MemberInitExpression node.

This is quite surprising since at least in the way I am used to using this library, vast majority of the expressions defining the document projections have the MemberInitExpression as the very root of the body of the Expression<Func<T1, T2>>, meaning that in this common case no constant folding would ever happen. Is that even possible? Or what am I missing here?

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Closing issues

To automatically close an issue: closes #1664

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error with Member Indexer when mapping value to a class model in select (if not const value)

1 participant