匿名托管的 DynamicMethods 程序集 c# linq

Anonymously Hosted DynamicMethods Assembly c# linq

我有一个问题:

_selectQuery = _selectQuery.Substring(0, _selectQuery.Length - 1) + ")";
    var testData= (from student in view.StudentView
     join  school in db.Schools on student.schoolid equals school.id into schools
    from sc in schools.DefaultIfEmpty()

    join  tr in db.Teacher on sc.id equals tr.schoolid into teacherSchools
    from tsc in teacherSchools.DefaultIfEmpty()
select new
{
school, sc, tsc
}.Select(_selectQuery);


 foreach (var item in testData)
{
   allData.Add(item.ToDynamic());
 }

上面的代码在 foreach/iteration 部分抛出异常:testData 为 null。

Anonymously Hosted DynamicMethods Assembly at lambda_method(Closure , <>f__AnonymousType33713 ) at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext()
at Swift.PeopleCommon.Data.Export.EnhancedExportService.GetGridData(GridJsonGetRows grid, Boolean limitData) at DynamicModule.ns.Wrapped_IEnhancedExportStore_a2d199ba35504f35a326f3807ad0f404.__1(IMethodInvocation inputs, GetNextInterceptionBehaviorDelegate getNext)

我试过像

这样的addung null checker
 join  school in db.Schools on student==null ? 0 : student.schoolid equals school.id into something

但仍然报错。

我尝试为 select 部分(例如 select new TestClass{})创建一个 class 而不是匿名的,但仍然抛出异常。我可能缺少什么?

检查 from tsc in teacherSchools.DefaultIfEmpty() 中的 tsc 是否为 NULL。

编辑 1:

我认为是抛出异常

select new { school, sc, tsc }

检查内部对象

select new 
{ 
   School= (school==null ? new School() : school),
   etc
}

我也遇到了同样的错误。原因是,其中一个数据库字段被设置为默认值,但数据库列的值为 NULL。所以我用默认字段更新了那个字段并且工作正常。