如何将以下lambda转换为表达式树?
source.Join(lookup, s => s.Id, l => l.Id, (s,l) => l)
我认为除了resultSelector(s,l)=> l外,我已经涵盖了所有内容。
这是我的代码..谢谢!
public static IQueryable<TLookup> GetLookupSource<T, TLookup, TKey>(this IQueryable<T> source, IQueryable<TLookup> lookup{
ParameterExpression s = Expression.Parameter(source.ElementType, "s");
Expression<Func<T, TKey>> outerKeySelector = Expression.Lambda<Func<T, TKey>>(Expression.PropertyOrField(s, "Id"), s);
ParameterExpression l = Expression.Parameter(lookup.ElementType, "l");
Expression<Func<TLookup, TKey>> innerKeySelector = Expression.Lambda<Func<TLookup, TKey>>(Expression.PropertyOrField(l, "Id"), l);
Expression<Func<T, TLookup, IQueryable<TLookup>>> resultSelector = null;//<---How to compose this
MethodInfo joinMethod = typeof(Queryable).GetMethods(BindingFlags.Static | BindingFlags.Public).Where(m => m.Name == "Join" && m.GetParameters().Length == 5).First();
var genericJoinMethod = joinMethod.MakeGenericMethod(typeof(T), typeof(TLookup), typeof(TKey), typeof(IQueryable<TLookup>));
var result = genericJoinMethod.Invoke(source, new object[] { source, lookup, outerKeySelector, innerKeySelector, resultSelector });
return (IQueryable<TLookup>)result;
}
最佳答案
我认为应该这样做:
var resultSelector = Expression.Lambda<Func<T, TLookup, TLookup>>(l, s, l);
关于c# - 表达式树的lambda,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10037495/