Linq Null Problem

When I fetch data from the following Linq query, it errors when it hits a record where the column is null.

Is there a way that I can handle this in the query so that it doesn't throw an exception? (perhaps changing nulls to zero length strings?)

Dim feedbacks = From fb As Object In listIn
            Let member = fb.GetType().GetProperty(groupMember).GetValue(fb).ToString()
            Order By member

Open in new window

LVL 1
DodsworthAsked:
Who is Participating?
 
käµfm³d 👽Commented:
OK, add another Let expression to account for this:

Dim feedbacks = From fb As Object In listIn
                Let temp = fb.GetType().GetProperty(groupMember).GetValue(fb)
                Let member = If(temp Is Nothing, String.Empty, temp.ToString())
                Order By member

Open in new window

0
 
käµfm³d 👽Commented:
Dim feedbacks = From fb As Object In listIn
                Where fb IsNot Nothing
                Let member = fb.GetType().GetProperty(groupMember).GetValue(fb).ToString()
                Order By member

Open in new window

0
 
BlueYonderCommented:
toString() cannot be used on null values.  Add 1 or 2

where !string.IsNullOrEmpty(fb)

where !fb.FieldToCheck.Equals(null)
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
DodsworthAuthor Commented:
I still need a record back when the column is null though.
0
 
BlueYonderCommented:
Remove .ToString()
0
 
DodsworthAuthor Commented:
so I can't change he nulls to zero length?
0
 
käµfm³d 👽Commented:
Which part is throwing the exception?
0
 
DodsworthAuthor Commented:
the .tostring
0
 
BlueYonderCommented:
update let member to
Let member = fb.GetType().GetProperty(groupMember).GetValue(fb) == null ? "" : fb.GetType().GetProperty(groupMember).GetValue(fb).ToString()
0
 
BlueYonderCommented:
Here is the complete updated statement

Dim feedbacks = From fb As Object In listIn
            Let member = fb.GetType().GetProperty(groupMember).GetValue(fb) == null ? "" : fb.GetType().GetProperty(groupMember).GetValue(fb).ToString()
            Order By member
0
 
käµfm³d 👽Commented:
@BlueYonder

I don't think C# syntax works in VB  ; )
0
 
BlueYonderCommented:
Sorry, just used to c#.  Here's the VB version

Dim feedbacks = From fb As Object In listIn
            Let member = If(fb.GetType().GetProperty(groupMember).GetValue(fb) Is Nothing, "", fb.GetType().GetProperty(groupMember).GetValue(fb))
            Order By member
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.