Link to home
Start Free TrialLog in
Avatar of Dodsworth
Dodsworth

asked on

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

Avatar of kaufmed
kaufmed
Flag of United States of America image

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

Avatar of BlueYonder
BlueYonder

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

where !string.IsNullOrEmpty(fb)

where !fb.FieldToCheck.Equals(null)
Avatar of Dodsworth

ASKER

I still need a record back when the column is null though.
Remove .ToString()
so I can't change he nulls to zero length?
Which part is throwing the exception?
the .tostring
update let member to
Let member = fb.GetType().GetProperty(groupMember).GetValue(fb) == null ? "" : fb.GetType().GetProperty(groupMember).GetValue(fb).ToString()
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
@BlueYonder

I don't think C# syntax works in VB  ; )
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