?
Solved

Linq Null Problem

Posted on 2014-03-25
12
Medium Priority
?
284 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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
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

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

800 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