Solved

Linq Null Problem

Posted on 2014-03-25
12
268 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 74

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

Expert Comment

by:käµfm³d 👽
ID: 39953034
Which part is throwing the exception?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 74

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 74

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now