Using System.Linq.Dynamic.Core
.
I have the following code on a Blazor component:
private void TestFunc()
{
List<DataRow> dataRows = new List<DataRow>()
{
new DataRow("Australia", "NSW", "a", 10),
new DataRow("Australia", "NSW", "b", 20),
new DataRow("Australia", "VIC", "no", 10),
new DataRow("New Zealand", "AUK", "no", 25),
new DataRow("New Zealand", "AUK", "no", 15)
};
List<string> groupByFields = new List<string>() { "FieldA" }; // Test variable. This will not be defined at compile-time
string aggregatorField = "MeasureA";
string aggregatorFunction = "Sum";
var groupings = dataRows.AsQueryable().GroupBy($"new ({string.Join(",", groupByFields)})");
var aggregatedGroups = groupings.Select($"new (Key, {aggregatorFunction}({aggregatorField}) AS Value)").ToDynamicArray();
Console.WriteLine(aggregatedGroups.Length);
// This is the line that throws
aggregatedGroups.ToList().ForEach(x => Console.WriteLine(x));
}
public record DataRow(string FieldA, string FieldB, string FieldC, double MeasureA);
protected override void OnInitialized()
{
base.OnInitialized();
TestFunc();
}
But when I try and render the component, get the following error:
Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Operation is not valid due to the current state of the object.
System.InvalidOperationException: Operation is not valid due to the current state of the object.
at System.Reflection.Emit.GenericTypeParameterBuilder.GetGenericParameterConstraints()
at Microsoft.CSharp.RuntimeBinder.SymbolTable.AddAggregateToSymbolTable(NamespaceOrAggregateSymbol parent, Type type)
at Microsoft.CSharp.RuntimeBinder.SymbolTable.LoadSymbolsFromType(Type type)
at Microsoft.CSharp.RuntimeBinder.SymbolTable.GetCTypeFromType(Type type)
at Microsoft.CSharp.RuntimeBinder.RuntimeBinder.GetArgumentType(ICSharpBinder p, CSharpArgumentInfo argInfo, Expression param, DynamicMetaObject arg, Int32 index)
at Microsoft.CSharp.RuntimeBinder.RuntimeBinder.CreateArgumentArray(ICSharpBinder payload, Expression[] parameters, DynamicMetaObject[] args)
at Microsoft.CSharp.RuntimeBinder.RuntimeBinder.BindCore(ICSharpBinder payload, Expression[] parameters, DynamicMetaObject[] args, DynamicMetaObject& deferredBinding)
at Microsoft.CSharp.RuntimeBinder.RuntimeBinder.Bind(ICSharpBinder payload, Expression[] parameters, DynamicMetaObject[] args, DynamicMetaObject& deferredBinding)
at Microsoft.CSharp.RuntimeBinder.BinderHelper.Bind(ICSharpBinder action, RuntimeBinder binder, DynamicMetaObject[] args, IEnumerable`1 arginfos, DynamicMetaObject onBindingError)
at Microsoft.CSharp.RuntimeBinder.CSharpInvokeMemberBinder.FallbackInvokeMember(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
at System.Dynamic.InvokeMemberBinder.FallbackInvokeMember(DynamicMetaObject target, DynamicMetaObject[] args)
at System.Dynamic.DynamicMetaObject.BindInvokeMember(InvokeMemberBinder binder, DynamicMetaObject[] args)
at System.Dynamic.InvokeMemberBinder.Bind(DynamicMetaObject target, DynamicMetaObject[] args)
at System.Dynamic.DynamicMetaObjectBinder.Bind(Object[] args, ReadOnlyCollection`1 parameters, LabelTarget returnLabel)
at System.Runtime.CompilerServices.CallSiteBinder.BindCore[Action`3](CallSite`1 site, Object[] args)
at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid2[Type,Object](CallSite site, Type arg0, Object arg1)
at StockAnalysis.WebAssembly.Pages.Index.<>c.<TestFunc>b__4_0(Object x) in C:\Users\Harry\source\repos\StockAnalysis\StockAnalysis.WebAssembly\Pages\Index.razor:line 75
at System.Collections.Generic.List`1[[System.Object, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].ForEach(Action`1 action)
at StockAnalysis.WebAssembly.Pages.Index.TestFunc() in C:\Users\Harry\source\repos\StockAnalysis\StockAnalysis.WebAssembly\Pages\Index.razor:line 75
at StockAnalysis.WebAssembly.Pages.Index.OnInitialized() in C:\Users\Harry\source\repos\StockAnalysis\StockAnalysis.WebAssembly\Pages\Index.razor:line 84
at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
Interstingly, the console app yields the length of aggregatedGroups
to be of length 2, whereas in web assembly, it is of length 1.
The code works perfectly fine in a console app which is strange (even with the same System.Linq.Dynamic.Core version == 1.2.12). I honestly don't know what to do. Any help would be greatly appreciated, thanks!