?
Solved

How to debug linq statement preferably VS 2010?

Posted on 2011-04-20
10
Medium Priority
?
391 Views
Last Modified: 2012-05-11
Hi there;

Could you tell me how to debug linq statement preferably VS 2010?

Kind regards.
0
Comment
Question by:jazzIIIlove
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 500 total points
ID: 35433131
Debugging LINQ queries can be problematic.  One of the reasons is that quite often, you write a large query as a single expression, and you can’t set a breakpoint mid-expression.  Writing large queries in expression context is particularly powerful when using functional construction to form XML (or using the strongly typed DOM in Open XML SDK V2).  This post presents a little trick that makes it easier to use the debugger with LINQ queries that are written using ‘method syntax’.

http://blogs.msdn.com/b/ericwhite/archive/2008/11/07/debugging-linq-queries.aspx


Debugging, Visualizing and Querying data using LINQ
http://blog.rajsoftware.com/post/Debugging-Visualizing-and-Querying-data-using-LINQ.aspx
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35433434
Hi there;

Ok, I downloaded and tried to use LinqPad4 but the issue is that although I imported the respective dll for a class that my query uses, I failed to execute the linq query. What should I do?

That class has the .cs file also, but it seems linq accepts exe and dll as reference only.

Kind regards.
0
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 35820580
Is it LINQ-to-SQL, LINQ-to-Entities, LINQ-to-XML, or LINQ-to-Objects?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35821608
LINQ-to-SQL
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 35822861
There is a debug visualizer for LINQ-to-SQL, as highlighted by Scott Guthrie:

LINQ to SQL Debug Visualizer
http://weblogs.asp.net/scottgu/archive/2007/07/31/linq-to-sql-debug-visualizer.aspx
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35823150
Hi there;

Ok, just for the sake of the question, Could you give any other solution for Linq2XML and Linq2Objects?

Kind regards.
0
 
LVL 21

Assisted Solution

by:Craig Wagner
Craig Wagner earned 500 total points
ID: 35823619
For LINQ-to-SQL you could use the debug visualizer or SQL Server Profiler. Bottom line, you need to capture the SQL that is being generated. From there you can tweak the SQL to get the results you want and then work backwards to the LINQ query.

There isn't really a way to "debug" any LINQ statements but it's easier when it's LINQ-to-SQL because then you can at least see the intermediate (i.e. the SQL query) result. With LINQ-to-XML and LINQ-to-Objects you're left with trial-and-error. The best thing you can do is read any error messages that are generated very carefully, they'll usually give you a clue as to what is wrong. If there's no error and you're just getting the wrong results you just need to study your query and figure out why you're getting what you're getting. There is no magic bullet.
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 1000 total points
ID: 35824363
There is one approach to debugging LINQ-to-XML, where you break the statement down into testable parts.

Debugging LINQ Queries
http://blogs.msdn.com/b/ericwhite/archive/2008/11/07/debugging-linq-queries.aspx

The gist of the technique is to insert a call to the Select extension method in the middle of your query.

var uniqueWords = text
    .Split(' ', '.', ',')
    .Where(i => i != "")
    .Select(i => i.ToLower())
    .GroupBy(i => i)
    .OrderByDescending(i => i.Count())
    .Select(i => new { Word = i.Key, Count = i.Count() })
    .Take(10);

Open in new window


split like this:

var uniqueWords = text
    .Split(' ', '.', ',')
    .Where(i => i != "")
    .Select(i => i.ToLower())
    .GroupBy(i => i)
    .Select(z => { return z; })
    .OrderByDescending(i => i.Count())
    .Select(i => new { Word = i.Key, Count = i.Count() })
    .Take(10);

Open in new window

0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 35828059
good.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses

621 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