Anonymously Hosted DynamicMethods Assembly c# linq

user742102 picture user742102 · Oct 15, 2015 · Viewed 7k times · Source

I have a query:

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

the code above throws exception in the foreach/iteration part: testData is 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)

I tried addung null checker like

 join  school in db.Schools on student==null ? 0 : student.schoolid equals school.id into something

but still throws error.

I tried creating a class for the select part(eg. select new TestClass{}) instead of anonymous but still throws exception. what could I be missing?

Answer

Hamix picture Hamix · Oct 15, 2015

Check if the tsc in your from tsc in teacherSchools.DefaultIfEmpty() is NULL or not.

Edit 1:

I think the exception is thrown in

select new { school, sc, tsc }

check inner object

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