Solved

Linq Null Problem

Posted on 2014-03-25
12
272 Views
Last Modified: 2016-02-10
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

0
Comment
Question by:Dodsworth
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39952975
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
 
LVL 9

Expert Comment

by:BlueYonder
ID: 39952984
toString() cannot be used on null values.  Add 1 or 2

where !string.IsNullOrEmpty(fb)

where !fb.FieldToCheck.Equals(null)
0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39953004
I still need a record back when the column is null though.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:BlueYonder
ID: 39953011
Remove .ToString()
0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39953030
so I can't change he nulls to zero length?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39953034
Which part is throwing the exception?
0
 
LVL 1

Author Comment

by:Dodsworth
ID: 39953038
the .tostring
0
 
LVL 9

Expert Comment

by:BlueYonder
ID: 39953074
update let member to
Let member = fb.GetType().GetProperty(groupMember).GetValue(fb) == null ? "" : fb.GetType().GetProperty(groupMember).GetValue(fb).ToString()
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39953078
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
 
LVL 9

Expert Comment

by:BlueYonder
ID: 39953080
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39953081
@BlueYonder

I don't think C# syntax works in VB  ; )
0
 
LVL 9

Expert Comment

by:BlueYonder
ID: 39953095
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question