首页 > 解决方案 > Linq 表达式在 Blazor 但不是控制台应用程序中引发错误

问题描述

使用System.Linq.Dynamic.Core.

我在 Blazor 组件上有以下代码:

    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();
    }

但是当我尝试渲染组件时,出现以下错误:

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()

有趣的是,控制台应用程序产生的长度为aggregatedGroups2,而在 Web 组装中,它的长度为 1。

该代码在奇怪的控制台应用程序中运行良好(即使使用相同的 System.Linq.Dynamic.Core 版本 == 1.2.12)。老实说,我不知道该怎么办。任何帮助将不胜感激,谢谢!

标签: c#linqexpressionblazorwebassembly

解决方案


推荐阅读