独自の式を作成してコンパイルする場合、または AsQueryable を使用する場合は、はい。LINQ で生成されたメソッドをデバッグするのは非常に面倒です。
実際のメソッドの小さな断片を使用することで、多少の手間を省くことができます- 少なくとも有用なものがスタック トレースに表示されます...
もう 1 つの考慮事項は次のとおりです。1 つの巨大な式を使用するのではなく、もう少しデイジー チェーン接続を行うことができれば、(スタック トレースから) どこで失敗しているかをより多く把握できる可能性があります。欠点はパフォーマンスです。Where(foo).Where(bar) は 2 つのデリゲート呼び出しですが、Where(foo && bar) は 1 つです。
1 つのオプションは、拡張メソッドのデバッグ バージョンを交換することです。IQueryable<T>
残念ながら、とが同じ名前空間にあるため、少し不便Queryable
です...これは機能しますが...
最初に出力:
>Where: x => ((x % 2) = 0)
<Where: x => ((x % 2) = 0)
>Count
'WindowsFormsApplication2.vshost.exe' (Managed): Loaded 'Anonymously Hosted DynamicMethods Assembly'
<Count
コード:
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;
}
}
}
}