Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Linq Null Problem

Posted on 2014-03-25
12
Medium Priority
?
296 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 2000 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

Independent Software Vendors: 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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

972 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