Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 65995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T19:02:02+00:00 2026-05-10T19:02:02+00:00

We are using LINQ very widely in our system. Particularly LINQ-to-objects. So in some

  • 0

We are using LINQ very widely in our system. Particularly LINQ-to-objects. So in some places we end up having a LINQ query in memory build up from some huge expressions. The problem comes when there’s some bug in the expressions. So we get NullReferenceException and the stack trace leads us nowhere (to [Lightweight Function]). The exception was thrown inside the dynamic method generated by LINQ.

Is there any easy way to debug such dynamic methods? Or do I have to sacrifice myself to learning WinDBG? 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T19:02:02+00:00Added an answer on May 10, 2026 at 7:02 pm

    If you are building your own expressions and compiling them, or using AsQueryable, then yes; the LINQ-generated methods will be a royal pain to debug.

    You can save some pain by using small fragements of actual methods – at least something useful will show in the stack trace…

    Another consideration is: rather than having one huge expression, if you can daisy-chain things a bit more you might have more idea (from the stack trace) where it is failing. The downside is performance – a Where(foo).Where(bar) is two delegate invokes, where-as Where(foo && bar) can be one.

    One option might be to swap in a debug version of the extension methods; unfortunately it is a little inconvenient because IQueryable<T> and Queryable are in the same namespace… this works, though…

    Output first:

    >Where: x => ((x % 2) = 0) <Where: x => ((x % 2) = 0) >Count 'WindowsFormsApplication2.vshost.exe' (Managed): Loaded 'Anonymously Hosted DynamicMethods Assembly' <Count 

    Code:

    using System; using System.Diagnostics; using System.Linq.Expressions;  namespace Demo {     using DebugLinq;     static class Program     {         static void Main()         {             var data = System.Linq.Queryable.AsQueryable(new[] { 1, 2, 3, 4, 5 });             data.Where(x => x % 2 == 0).Count();          }     } } namespace DebugLinq {     public static class DebugQueryable     {         public static int Count<T>(this System.Linq.IQueryable<T> source)         {             return Wrap(() => System.Linq.Queryable.Count(source), 'Count');         }          public static System.Linq.IQueryable<T> Where<T>(this System.Linq.IQueryable<T> source, Expression<Func<T, bool>> predicate)         {             return Wrap(() => System.Linq.Queryable.Where(source, predicate), 'Where: ' + predicate);         }         static TResult Wrap<TResult>(Func<TResult> func, string caption)         {             Debug.WriteLine('>' + caption);             try             {                 TResult result = func();                 Debug.WriteLine('<' + caption);                 return result;             }             catch             {                 Debug.WriteLine('!' + caption);                 throw;             }         }     } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple table and I can happily query it using LINQ
I'm using LINQ to SQL to query my database, I have a query very
I've written this very simple function to replace a file extension using LINQ in
Using LINQ to Entities sounds like a great way to query against a database
I am using LINQ to query a generic dictionary and then use the result
We've just started using LINQ to SQL at work for our DAL & we
I feel like this is not a very efficient way of using linq. I
using Linq-to-SQL I'd like to prefetch some data. 1) the common solution is to
Using LINQ to SQL, I have an Order class with a collection of OrderDetails.
Using LINQ to SQL db.Products.Where(c => c.ID == 1).Skip(1).Take(1).ToList(); executes SELECT [t1].[ID], [t1].[CategoryID], [t1].[Name],

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.