Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net Linq中的泛型Select子句_.net_Linq_Entity Framework_Generics_Entity Framework 4 - Fatal编程技术网

.net Linq中的泛型Select子句

.net Linq中的泛型Select子句,.net,linq,entity-framework,generics,entity-framework-4,.net,Linq,Entity Framework,Generics,Entity Framework 4,我见过通用存储库模式。我正在尝试添加我想作为调用函数的参数发送的泛型select子句。我修改了 public T GetSingle(Expression<Func<filter, bool>> filter) to public T GetSingle(Expression<Func<filter, bool>> filter,Expression<Func<T, T>> Selct) 方法中的第二个参数必须是另一个l

我见过通用存储库模式。我正在尝试添加我想作为调用函数的参数发送的泛型select子句。我修改了

public T GetSingle(Expression<Func<filter, bool>> filter) to 
public T GetSingle(Expression<Func<filter, bool>> filter,Expression<Func<T, T>> Selct)

方法中的第二个参数必须是另一个lambda,其中输入为T,并且必须返回T。

当前,方法无法更改结果类型。我想那不是你想要的。我希望您希望您的方法是通用的,例如

public TResult GetSingle<TResult>(Expression<Func<T, bool>> filter,
                                  Expression<Func<T, TResult>> projection)

我们不知道你想要达到什么目的,这使得你很难知道你想要通过什么论点。是否确实需要
表达式而不是更改结果类型的功能?(
Expression
)实际上,我想从存储库发送一个字段名,我的查询应该返回该字段,而不是要以字符串或lambda表达式形式在字段名中传递的整个对象?(请阅读并编辑您的问题,这样我们就不需要反复讨论了。)
表达式将t作为输入并返回t作为输出,而不允许您从t中选择特定属性,您也可以像
repository.GetSingle那样使用它(p=>p.Slug==Slug,p=>new{p.FirstName,p.SecondName})
。它在TResult上给了我错误信息。说类型或名称空间不是found@Tassadaque:您确定已将我的更改正确复制到方法签名吗
TResult
是该方法的泛型类型参数。(碰巧,我把方法的返回类型搞错了,但这不能解释您看到的错误。)是的,您可以为匿名类型调用它。
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);