Solved

linq how to debug selecting objects into new objects

Posted on 2013-05-25
3
194 Views
Last Modified: 2013-05-29
Hi,
I have a ton of xml web service responses which were converted into classes.  There are a few issues with these as we are seeing.  I commonly do something like the following to display some classes in a grid.  Sometimes depending on the input, everything blows up "Object reference not set to an instance of an object.".  When this happens, how can i go about trying to debug this?  I am guessing there is a null value or something.  I am looking for something reusable.

Thanks!


                var results = (from os in orderStatus.orderstatus
                               select new
                               {
                                   orderNumber = os.orderNumber,
                                   reportedTime = os.reportedTime,
                                   orderstring = os.orderstring,
                                   symbol = os.order.symbol,
                                   quantity = os.order.quantity,
                               }).ToList();
0
Comment
Question by:jackjohnson44
  • 2
3 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39197344
change it to this:
   var results = (from os in orderStatus.orderstatus
                               select new
                               {
                                   orderNumber = os.orderNumber,
                                   reportedTime = os.reportedTime,
                                   orderstring = os.orderstring,
                                   symbol = os.order ! =null ? os.order.symbol : null,
                                   quantity = os.quantity != null ? os.order.quantity : null,
                               }).ToList();

Open in new window

0
 

Author Comment

by:jackjohnson44
ID: 39198012
Thanks, I am more looking for a way to determine which element is having the issue.  i have several of these and want to know what the issue is instead of just checking for null on each element.
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39198173
So dont linq but simple foreach.
Add exception handling inside the loop and log iteration that threw exception.
For each(var os in orderStatus.orderstatus){
Try{

}catch(Exception ex){
//log exception and os details her
}
}
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

13 Experts available now in Live!

Get 1:1 Help Now