Solved

linq how to debug selecting objects into new objects

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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…

856 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