Solved

linq how to debug selecting objects into new objects

Posted on 2013-05-25
3
193 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 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

10 Experts available now in Live!

Get 1:1 Help Now