Results 1 to 3 of 3

Thread: [RESOLVED] LINQ null return issue

  1. #1

    Thread Starter
    Fanatic Member Seraph's Avatar
    Join Date
    Jul 2007
    Posts
    959

    Resolved [RESOLVED] LINQ null return issue

    I'm having trouble figuring out what to do with the bolded part, when there are no nodes to return.
    If there are no image tags inside the <images> tag, i get an "Object reference is not set to an instance of an object".
    I have been searching and searching for an answer but can't find one.

    Code:
                Dim orders = From o In xmlLinq.Descendants("order")
                Select New With { _
                    .orderID = o.Attribute("id").Value, _
                    .userID = o.Element("user").Element("userid").Value, _
                    .firstname = o.Element("user").Element("firstname").Value, _
                    .lastname = o.Element("user").Element("lastname").Value, _
                    .username = o.Element("user").Element("username").Value, _
                    .password = o.Element("user").Element("password").Value, _
                    .address_1 = o.Element("user").Element("address_1").Value, _
                    .address_2 = o.Element("user").Element("address_2").Value, _
                    .city = o.Element("user").Element("city").Value, _
                    .state = o.Element("user").Element("state").Value, _
                    .postal = o.Element("user").Element("postal").Value, _
                    .email = o.Element("user").Element("email").Value, _
                    .phone = o.Element("user").Element("phone").Value, _
                    Key .ads = From a In o.Descendants("ad")
                               Where a.Attribute("type").Value = "Web"
                                Select New With { _
                                    .type = a.Attribute("type").Value, _
                                    .headline = a.Element("text").Element("headline").Value, _
                                    .body = a.Element("text").Element("body").Value, _
                                    .run_length = a.Element("regions").Element("region").Element("schedule").Attribute("run_length").Value, _
                                    .start_date = a.Element("regions").Element("region").Element("schedule").Attribute("start_date").Value, _
                                    Key .images = From i In a.Descendants("images")
                                                  Select New With { _
                                                      .image = i.Element("image").Value _
                                                  }
                                }
                }

    Visual Studio 2010 Professional | .NET Framework 4.0 | Windows 7

    SERYSOFT.COM :: SysPad - Folder Management Program - Please comment HERE if you find this program useful, have ideas, or know of any bugs.
    [Very useful for IT/DP departments where many folders are consistently accessed. Also contains a scratchpad window for quick access to notes.]

    [.NET and MySQL Quick Guide]

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: LINQ null return issue

    Something like:
    vb.net Code:
    1. .image = If(i.Element("image") Is Nothing, CObj(Nothing), i.Element("image").Value)
    The CObj may or may not work. If not then you'll have to cast the null reference as the appropriate type.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Seraph's Avatar
    Join Date
    Jul 2007
    Posts
    959

    Re: LINQ null return issue

    Cool, thanks...that worked perfect. I had thought about an If, but I wasn't sure what would go in the TRUE portion of it.

    Visual Studio 2010 Professional | .NET Framework 4.0 | Windows 7

    SERYSOFT.COM :: SysPad - Folder Management Program - Please comment HERE if you find this program useful, have ideas, or know of any bugs.
    [Very useful for IT/DP departments where many folders are consistently accessed. Also contains a scratchpad window for quick access to notes.]

    [.NET and MySQL Quick Guide]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width