c# - 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()
有趣的是,控制台应用程序产生的长度为aggregatedGroups
2,而在 Web 组装中,它的长度为 1。
该代码在奇怪的控制台应用程序中运行良好(即使使用相同的 System.Linq.Dynamic.Core 版本 == 1.2.12)。老实说,我不知道该怎么办。任何帮助将不胜感激,谢谢!
解决方案
推荐阅读
- swift - UICollectionView 在其每个 UICollectionViewCells 中都有一个 UIImageView 消耗过多的 RAM 内存
- python - 在 Python 熊猫中转义“\N”
- python - Selenium 和 Pandas 在页面中抓取表格
- r - 在 R 中使用 facet_wrap 创建密度图,添加代表其他列的线和数据点
- javascript - 如何使用代码生成来做 Angular 类型的表单
- regex - 电话号码 (XXX) XXXXXXX 的正则表达式是什么
- python-3.x - NER:为 Spacy v3 定义训练数据
- ios - 合并发布者时如何处理错误?
- python - Heroku - 上传 Django-App 时应用与 buildpack 不兼容 https://github.com/heroku/heroku-buildpack-python.git
- node.js - JSON.parse() 出现意外错误:“JSON 输入意外结束”