如何在linq中调试`Null引用异常?

时间:2013-10-08 13:23:14

标签: c# linq

string search = textBoxNachname.Text;
var Liste = _db.T_Subscribers
                .Where(x => x.firstname.StartsWith(search))
                .Except(_selectedcourse.T_Coursedetail.Select(b => b.T_Subscribers))
                .Where(M => M.T_Tln_Student == null || M.T_Tln_Stud.Status.T_Status.T_Statusart == _studentEx).ToList();

我已经编写了上面的代码来提取名称以文本框中的搜索元素开头的列表...然后我需要排除已经注册课程的名称,然后如果他们不是机构(M => M.T_Tln_Student == null)和前学生包括在列表中..

但是我发现Null引用异常......

2 个答案:

答案 0 :(得分:4)

这是你可以调试它的方法:

var Liste1 = _db.T_Subscribers.Where(x => x.firstname.StartsWith(search));
var Liste2 = Liste1.Except(
               _selectedcourse.T_Coursedetail.Select(b => b.T_Subscribers));
var Liste3 = Liste2.Where(M =>
                M.T_Tln_Student == null ||
                M.T_Tln_Stud.Status.T_Status.T_Statusart == _studentEx);
var Liste = Liste3.ToList();

重点是使用这种技术来分割东西。

答案 1 :(得分:0)

看看这一行:

.Where(M => M.T_Tln_Student == null || 
            M.T_Tln_Stud             // might be null
                        .Status      // might be null
                        .T_Status    // might be null
                        .T_Statusart // might be null
                           == _studentEx)

我建议你在这里开始搜索NullReferenceException

.Where(M => M.T_Tln_Student == null || 
            M.T_Tln_Stud == null ||
            M.T_Tln_Stud.Status == null||
            M.T_Tln_Stud.Status.T_Status == null ||
            M.T_Tln_Stud.Status.T_Status.T_Statusart == null ||
            M.T_Tln_Stud.Status.T_Status.T_Statusart == _studentEx)