Note that I have added an internal expression. There is some closing code that you will need to do as you have a captured variable.
Pay attention to the possibility of simplification when using the code fooobar.com/questions/125802 / ....
Minimal code to get expression code in 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)); } } }
Here is what I see in the reflector:
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 }); }
Close enough for government work ... This tells me that this is far from trivial, and you need to simplify your initial request.
I would also try running LinqPad for rapid prototyping.
Gregc source share