Geeks With Blogs

David Madden Mastering the craft one mountain at a time.
In recent training with another staff member, we found LINQ code that worked well in LINQPad but failed in VisualStudio 2010.  Not a syntax failure but in that the query would return no value.

The programmer was new to LINQ and making strides in trying different things.  As old habits die hard, local variables where used to make an assignment to the class that would hold the information returned from the LINQ query.  In the foreach block, assignment to the final object was being made to the local variables instead of the query's projection output.

The solution was to remove the local variables above the query, use select new { ... } to determine the named output, and then use a foreach block to output the results of the query into the final object created from a custom class.

I should have caught this sooner but I had mistakenly assumed that something was not right with the query.  The query had some interesting lambda expressions that I had assumed (again) that might have been causing the problem.

Lesson learned?  Check to see how assignment is made to the final object first.
Posted on Monday, July 23, 2012 1:51 PM | Back to top

Comments on this post: Scooby Doo Mystery LINQ

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © DavidMadden | Powered by: