请注意,我添加了内部表达式。您需要编写一些闭包代码,因为您有一个捕获的变量。
请注意使用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 进行快速原型设计