1
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();

I have written the above piece of code to extract a list whose name starts with the Search element in textbox...then I need to exclude the names who have already enrolled for the course, then if they are not the students of the Institution (M => M.T_Tln_Student == null) and ex-students include in the list..

But I am getting Null reference exception occurred...

12
  • 1
    Have you tried sticking a breakpoint in the expressions to see what is null? Any answer to this could only make guesses... Commented Oct 8, 2013 at 13:25
  • 2
    you can't add breakpoints inside a linq query. The error you get means that 1 of your parameters is null. Commented Oct 8, 2013 at 13:26
  • 1
    @patxy Is that a linq to sql limitation? I do it with linq to objects frequently. Commented Oct 8, 2013 at 13:26
  • 2
    @patxy Ah, I guess you mean by clicking in the side-bar, which you are correct will highlight the entire row. I am not referring to this. Have you tried right clicking the expression content itself and doing "Breakpoint -> Insert Breakpoint"? I might have just made your life a little easier... I wish people would check the facts before up-voting incorrect things. Commented Oct 8, 2013 at 13:29
  • 1
    @patxy I cannot speak for Linq to SQL as I never use it, I only use Linq to Objects, hence my initial question for clarification. Personally I would break the expressions out into method groups that can be debugged a little more clearly. Commented Oct 8, 2013 at 13:31

2 Answers 2

4

This is how you can debug this:

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

The focus is to use this technique to split things.

Sign up to request clarification or add additional context in comments.

Comments

0

look at this line:

.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)

I would suggest that you start your search for the NullReferenceException here

.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)

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.