2015-10-15 90 views
1

我有一个查询:匿名托管DynamicMethods大会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 /迭代部分:TESTDATA为空。

匿名在 Swift.PeopleCommon.Data.Export.EnhancedExportService.GetGridData(GridJsonGetRows 网格,布尔limitData)在 托管DynamicMethods大会lambda_method(封闭 ,<> f__AnonymousType337 13) at System.Linq.Enumerable.WhereSelectEnumerableIterator 2.MoveNext()
DynamicModule.ns.Wrapped_IEnhancedExportStore_a2d199ba35504f35a326f3807ad0f404 .__ 1(IMethodInvocation 输入,GetNextInterceptionBehaviorDelegate GetNext)的

我试过addung null checker like

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

但仍然抛出错误。

我试着创建一个类的选择部分(例如选择新的TestClass {}),而不是匿名,但仍然会引发异常。我可能会错过什么?

回答

0

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

编辑1:

我觉得异常在

select new { school, sc, tsc } 

检查内部对象

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

这应该是一个评论,而不是一个答案。 –

+0

使用泛型助手扩展方法“IfNotNull”。看到这个链接http://stackoverflow.com/a/854619/1476708 – Hamix

+0

我是否正确使用它? (g => g.id)等于tr.ifNotNull(g => g.schoolid)into teacherSchools 尽管如此,它仍会引发相同的错误。 – user742102

0

我也得到了同样的错误抛出。原因是,其中一个db字段被设置为默认值,但db列的值为NULL。所以我有更新该字段与默认字段,并工作得很好。