将一些 LINQ 查询传递到函数的参数中?
我有以下 C# 代码示例,演示了如何将一些 LINQ 查询解析为函数的参数。
public List<object> AllElements;
public object GetAll<T>(SomeLINQQuery) {
//get some elements from AllElements where the argument is added to the query, as shown below.
}
现在,为了赋予它一些意义,我想要实现的目标是:
public void test() {
GetAll<object>(where object.ToString() == "lala");
}
这有点难以解释。我希望这个例子能做得很好。
I have the following code sample in C# demonstrating how I would like to parse some of a LINQ query into a function's argument.
public List<object> AllElements;
public object GetAll<T>(SomeLINQQuery) {
//get some elements from AllElements where the argument is added to the query, as shown below.
}
And now, to give this some meaning, what I was thinking of accomplishing would be this:
public void test() {
GetAll<object>(where object.ToString() == "lala");
}
It's kind of hard to explain. I hope this example does it well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然。你会这样做:
你会这样调用它:
你甚至可以将它创建为扩展方法:
并这样调用它:
但实际上,在你的简单示例中,它没有任何意义,因为它完全是与直接使用
Where
相同:但是,如果您的
GetAll
方法执行更多操作,则将谓词传递给该方法可能是有意义的。Sure. You would do it like this:
You would call it like this:
You could even create it as an extension method:
and call it like this:
But really, in your simple example, it doesn't make any sense, because it is exactly the same as using
Where
directly:However, if your
GetAll
method does some more stuff, it could make sense to pass the predicate to that method.您可以将某种谓词传递给该方法:
You could pass some sort of predicate to the method: