1

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?

user742102
  • 1,335
  • 8
  • 32
  • 51

2 Answers2

1

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
}
Hamix
  • 1,323
  • 7
  • 18
  • This should be a comment, not an answer. – Ron Beyer Oct 15 '15 at 15:12
  • use generic helper extension method "IfNotNull". see this link http://stackoverflow.com/a/854619/1476708 – Hamix Oct 15 '15 at 15:19
  • did I use it correctly like this? join tr in db.Teacher on sc.ifNotNull(g=> g.id) equals tr.ifNotNull(g=> g.schoolid ) into teacherSchools it still throws the same error though. – user742102 Oct 15 '15 at 16:02
  • I also used it in the select statement (eg. school = s.isnotNull(s=> s)) still throws the same error. sorry not really sure what's going on coz I can see during debug that one of my left join is null, but isn't the null checker supposed to handle that? – user742102 Oct 15 '15 at 16:17
1

I also got same error. Reason was, one of the db field was set default but db column has value NULL. So i have update that field with default field and that worked fine.