LINQ to Entities - Checking for a null object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Joe Campanini
    New Member
    • May 2011
    • 58

    LINQ to Entities - Checking for a null object

    I'm using LINQ to Entities in a C# program with the following code...

    Code:
    ...
    
    var prof = (from p in de.ProfT
                where p.ProfT == ProfTitle
                select p).FirstOrDefault();
    
    if (prof.Title != null)
    {
    
    Do some stuff
    
    }
    I get a run time exception "Object reference not set to an instance of an object" How do I check if prof.Title not equal to null?

    I'm sorry if I'm a bit of a pain with all my questions, believe me I'm not stupid :-D, but I have no other way of learning all this good stuff.

    Joe
  • Curtis Rutland
    Recognized Expert Specialist
    • Apr 2008
    • 3264

    #2
    Code:
    where field.Property != null && field.Property == desiredValue

    Comment

    • Joe Campanini
      New Member
      • May 2011
      • 58

      #3
      Hi Curtis, the problem arises in the "Do some stuff..."

      Code:
      ...
      
      nameInfo[1] = prof.Title + " " + name.Firstname + " " + name.Surname;
      I get a run time exception "Object reference not set to an instance of an object" on the prof.Title because this particular body does not have a prof.Title

      I tried your code but I still get the same error

      Joe

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        Hmm, I assumed your error was in your select query.

        Anyway, there is an operator that is called the "null coalescing operator". It works like this:

        Code:
        string x = null;
        string y = x ?? string.Empty;
        That's the equivalent of this:

        Code:
        string x = null;
        string y;
        if(x == null)
          y = string.Empty;
        else 
          y = x;
        What it does: use the provided value only if it is not null. Otherwise, it uses the replacement value (on the right of the ??).

        So you could conceivably do something like this:

        Code:
        nameInfo[1] = prof.Title ?? string.Empty + " " + name.Firstname ?? string.Empty+ " " + name.Surname ?? string.Empty;
        Though I wouldn't suggest it. I wouldn't build the string like that. I'd use a StringBuilder:

        Code:
        using System.Text;
        ...
        StringBuilder sb = new StringBuilder();
        if(!string.IsNullOrWhiteSpace(myString1))
          sb.Append(string1 + " ");
        if(!string.IsNullOrWhiteSpace(myString2))
          sb.Append(string2 + " ");
        if(!string.IsNullOrWhiteSpace(myString3))
          sb.Append(string3 + " ");
        That way, you're not adding the spaces whether or not the strings are null. Only if they exist (and aren't empty).

        Comment

        • Joe Campanini
          New Member
          • May 2011
          • 58

          #5
          Thanks Curtis, youre a genius, I haven't even heard of a StringBuilder, :-D, I learn something new everyday. But I have been adviced that whatever it is you want to do there is a class out there somewhere that will do it, and it's proving, so far, to be true. Once again thanks. How long have you been doing this stuff??

          Joe

          Comment

          • Curtis Rutland
            Recognized Expert Specialist
            • Apr 2008
            • 3264

            #6
            C# for a few years now, but programming for about 10 years.

            Comment

            Working...