我有扩展方法:
public static IQueryable<TResult> WithFieldLike<TResult>( this IQueryable<TResult> query,Func<TResult,string> field,string value) { Expression<Func<TResult,bool>> expr = trans => field(trans).Contains(value); return query.Where(expr); }
我需要更改参数字段来键入:Expression>.会是这样的.
public static IQueryable<TResult> WithFieldLike<TResult>( this IQueryable<TResult> query,Expression<Func<TResult,string>> field,bool>> expr = ??? return query.Where(expr); }
var query7 = query.WithFieldLike(trans => trans.DeviceModelNumber,"ber_3");
在这种情况下我应该如何构建“expr”?请帮忙.
解决方法
解构字段并创建一个新表达式,如下所示:
var expr = Expression.Lambda<Func<TResult,bool>> ( Expression.Call (field.Body,typeof (string).GetMethod ("Contains"),Expression.Constant (value)),field.Parameters) ;
(根据Maxs在评论中的细化编辑)