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?)
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
toString() cannot be used on null values. Add 1 or 2
where !string.IsNullOrEmpty(fb)
where !fb.FieldToCheck.Equals(nu ll)
where !string.IsNullOrEmpty(fb)
where !fb.FieldToCheck.Equals(nu
ASKER
I still need a record back when the column is null though.
Remove .ToString()
ASKER
so I can't change he nulls to zero length?
Which part is throwing the exception?
ASKER
the .tostring
update let member to
Let member = fb.GetType().GetProperty(g roupMember ).GetValue (fb) == null ? "" : fb.GetType().GetProperty(g roupMember ).GetValue (fb).ToStr ing()
Let member = fb.GetType().GetProperty(g
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here is the complete updated statement
Dim feedbacks = From fb As Object In listIn
Let member = fb.GetType().GetProperty(g roupMember ).GetValue (fb) == null ? "" : fb.GetType().GetProperty(g roupMember ).GetValue (fb).ToStr ing()
Order By member
Dim feedbacks = From fb As Object In listIn
Let member = fb.GetType().GetProperty(g
Order By member
@BlueYonder
I don't think C# syntax works in VB ; )
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().GetPropert y(groupMem ber).GetVa lue(fb) Is Nothing, "", fb.GetType().GetProperty(g roupMember ).GetValue (fb))
Order By member
Dim feedbacks = From fb As Object In listIn
Let member = If(fb.GetType().GetPropert
Order By member
Open in new window