内部表現を追加したことに注意してください。キャプチャされた変数があるため、作成する必要があるクロージャー コードが少しあります。
https://stackoverflow.com/a/3472250/90475のコードを使用する場合の単純化の機会に注意してください。
DebugView で式コードを取得するための最小限のコード...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
namespace ConsoleApplication7
{
class CTX
{
public List<Customer> Invoices = new List<Customer>();
}
class Customer
{
public int id;
public static bool HasMatchingIds(Customer first, Customer second) { return true; }
}
class Program
{
static void Main(string[] args)
{
CTX ctx = new CTX();
Expression<Func<Customer, bool>> expression = cust => ctx.Invoices.Any(customer => Customer.HasMatchingIds(customer, cust));
}
}
}
リフレクターで見るとこんな感じ。
private static void Main(string[] args)
{
ParameterExpression CS$0$0000;
ParameterExpression CS$0$0002;
CTX ctx = new CTX();
Expression<Func<Customer, bool>> expression = Expression.Lambda<Func<Customer, bool>>(
Expression.Call(null, (MethodInfo) methodof(Enumerable.Any),
new Expression[] { Expression.Constant(ctx.Invoices), Expression.Lambda<Func<Customer, bool>>(
Expression.Call(null, (MethodInfo) methodof(Customer.HasMatchingIds), new Expression[] {
CS$0$0002 = Expression.Parameter(typeof(Customer), "customer"),
CS$0$0000 = Expression.Parameter(typeof(Customer), "cust") }),
new ParameterExpression[] { CS$0$0002 }) }), new ParameterExpression[] { CS$0$0000 });
}
政府の仕事に十分近い...これは、それが些細なことではないことを示しており、元のクエリを単純化する必要があります。
また、迅速なプロトタイピングのためにLinqPadを実行してみます