Skip to content

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

@jbhelm

Description

@jbhelm

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!

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions