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?

2 Answers 2

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

This should be a comment, not an answer.
use generic helper extension method "IfNotNull". see this link stackoverflow.com/a/854619/1476708
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.
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?
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.