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

Select("...") from an IQueryable of anonymous objects created via Select("new { ... }") throws InvalidOperationException #907

Open
jbhelm opened this issue Mar 27, 2025 · 0 comments

Comments

@jbhelm
Copy link

jbhelm commented Mar 27, 2025

Description

Using Select("...") on an IQueryable of anonymous object elements that were created by Select("new { ... }") throws System.InvalidOperationException: 'The result of the dynamic binding produced by the object with type '<>f__AnonymousType0`1' for the binder 'System.Linq.Dynamic.Core.DynamicGetMemberBinder' needs at least one restriction.'

Note that using Select("...") on an IQueryable of anonymous object elements that were created statically in code works as expected.

Exception

System.InvalidOperationException
  HResult=0x80131509
  Message=The result of the dynamic binding produced by the object with type '<>f__AnonymousType0`1' for the binder 'System.Linq.Dynamic.Core.DynamicGetMemberBinder' needs at least one restriction.
  Source=System.Linq.Expressions
  StackTrace:
   at System.Dynamic.DynamicMetaObjectBinder.Bind(Object[] args, ReadOnlyCollection`1 parameters, LabelTarget returnLabel) in /_/src/libraries/System.Linq.Expressions/src/System/Dynamic/DynamicMetaObjectBinder.cs:line 116
   at System.Runtime.CompilerServices.CallSiteBinder.BindCore[T](CallSite`1 site, Object[] args) in /_/src/libraries/System.Linq.Expressions/src/System/Runtime/CompilerServices/CallSiteBinder.cs:line 120
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) in /_/src/libraries/System.Linq.Expressions/src/System/Dynamic/UpdateDelegates.Generated.cs:line 115
   at System.Linq.Enumerable.SelectArrayIterator`2.MoveNext() in /_/src/libraries/System.Linq/src/System/Linq/Select.cs:line 191
   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items) in /_/src/libraries/Common/src/System/Collections/Generic/LargeArrayBuilder.SpeedOpt.cs:line 112
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source) in /_/src/libraries/Common/src/System/Collections/Generic/EnumerableHelpers.Linq.cs:line 84
   at System.Linq.Dynamic.Core.DynamicEnumerableExtensions.CastToArray[T](IEnumerable source)
   at System.Linq.Dynamic.Core.DynamicEnumerableExtensions.ToDynamicArray(IEnumerable source)
   at DynamicLinqExamples.SelectProjection.Execute() in C:\Users\JoshuaHelm\dev\DynamicLinqBugExamples\SelectProjection.cs:line 23
   at DynamicLinqExamples.Program.Main(String[] args) in C:\Users\JoshuaHelm\dev\DynamicLinqBugExamples\Program.cs:line 11

Repro

// works
var staticData = new[] { 1, 2 }
	.AsQueryable()
	.Select(x => new { Value = x })
	.ToDynamicArray();
var staticResult = staticData
	.AsQueryable()
	.Select("Value")
	.ToDynamicArray();

// throws System.InvalidOperationException: 'The result of the dynamic binding produced by the object with type '<>f__AnonymousType0`1' for the binder 'System.Linq.Dynamic.Core.DynamicGetMemberBinder' needs at least one restriction.'
var dynamicData = new[] { 1, 2 }
	.AsQueryable()
	.Select("new { it as Value }")
	.ToDynamicArray();
var dynamicResult = dynamicData
	.AsQueryable()
	.Select("Value")
	.ToDynamicArray();

Technical details

  • System.Linq.Dynamic.Core 1.6.0.2
  • .NET 8.0 and 9.0

Thanks!

@jbhelm jbhelm changed the title Select("...") from an IQueryable of anonymous objects created via Select("new { ... }") fields with InvalidOperationException Select("...") from an IQueryable of anonymous objects created via Select("new { ... }") throws InvalidOperationException Mar 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant