我见过泛型存储库模式here。我正在尝试添加泛型select子句,我希望将其作为调用函数的参数发送。我修改过了
public T GetSingle(Expression<Func<filter, bool>> filter) to
public T GetSingle(Expression<Func<filter, bool>> filter,Expression<Func<T, T>> Selct)
现在我想调用这个方法。我应该如何发送select表达式
return rep.GetSingle(p => p.Slug == slug,???)
发布于 2012-06-27 13:48:50
目前,您的方法不能更改结果类型。我怀疑这不是你想要的。我希望你希望你的方法是泛型的,例如
public TResult GetSingle<TResult>(Expression<Func<T, bool>> filter,
Expression<Func<T, TResult>> projection)
然后你可以像这样调用它:
// The type argument is inferred from the second lambda expression
string x = repository.GetSingle(p => p.Slug == slug, p => p.FirstName);
发布于 2012-06-27 13:44:36
方法中的第二个参数必须是另一个lambda,其中输入是T,并且它必须返回T。
https://stackoverflow.com/questions/11220053
复制相似问题