简体   繁体   English

什么表达 <Func<T,bool> &gt;声明是什么意思

[英]What does the Expression<Func<T,bool>> declaration mean?

Could somebody explain the following declaration in a way that conveys the meaning of the expression and how it would be called? 有人可以用一种方式解释下面的声明,它表达了表达式的含义以及如何调用它?

void Delete<T>(Expression<Func<T, bool>> expression) where T : class, new();

I read it as: Delete an object of type T , by passing in a lambda expression whose parameter is an object of type T that returns a bool . 我将其读作:通过传入一个lambda表达式来删除类型为T的对象,该表达式的参数是返回bool的类型为T的对象。

Also, can you replace Func<T, bool> expression with Predicate<T> expression 此外,你可以用Predicate<T> expression替换Func<T, bool> expression Predicate<T> expression

This method is probably a member of a collection type, yes? 这个方法可能是集合类型的成员,是吗?

A "predicate" is any device that says "yes" or "no" to the question "is this thing a member of that set?" “谓词”是对问题“是”还是“否”的任何设备“这个东西是该集合的成员吗?” So a predicate for the set "integers even positive integers" would be x=> x > 0 && x % 2 == 0 . 所以集合“整数甚至正整数”的谓词将是x=> x > 0 && x % 2 == 0

This method probably has the semantics of "delete from the collection all members of the collection that are in the set identified by the predicate". 此方法可能具有“从集合中删除集合中由谓词标识的集合中的所有成员”的语义。

The predicate is passed to the method in the form of an expression tree, which is a way of passing the structure of the predicate in a manner that can be analyzed at runtime and transformed. 谓词以表达式树的形式传递给方法,表达式树是一种以可在运行时分析并转换的方式传递谓词结构的方法。 It is typically used in scenarios where the "collection" is actually a database somewhere, and the deletion request needs to be translated into a query in the database's query language and sent over the network. 它通常用于“集合”实际上是某个地方的数据库的情况,并且删除请求需要以数据库的查询语言转换为查询并通过网络发送。

The first is a method which accepts an expression tree (not necessarily created from a lambda expression tree). 第一种是接受表达式树的方法(不一定是从lambda表达式树创建的)。 The expression tree represents an expression which accepts a T and returns a bool . 表达式树表示接受T并返回bool的表达式。 T is constrained to be a reference type with a parameterless constructor. T被约束为具有无参数构造函数的引用类型。

As for the semantic meaning - that's up to the documentation/implementation. 至于语义含义 - 这取决于文档/实现。

It's important to distinguish between a lambda expression, which is one way of creating an expression tree, and an expression tree itself. 区分lambda表达式和表达式树本身很重要,lambda表达式是创建表达式树的一种方式。

As for whether it could use Predicate<T> instead - maybe. 至于它是否可以使用Predicate<T> - 也许。 It depends on what the implementation does with it. 这取决于实现对它的作用。 They represent the same delegate signature , certainly - but you can't convert between the two types of expression tree trivially. 它们代表相同的委托签名 ,当然 - 但是你不能轻易地在两种类型的表达式树之间进行转换。

this methods gets as a parameter expression tree of function that gets object with public parameter-less constructor and returns boolean. 此方法作为函数的参数表达式树获取,使用公共无参数构造函数获取对象并返回boolean。

you can read more about expression trees and their usage here: http://msdn.microsoft.com/en-us/library/bb397951.aspx 您可以在此处阅读有关表达式树及其用法的更多信息: http//msdn.microsoft.com/en-us/library/bb397951.aspx

While the method signature looks invalid to me, essentially you are passing in an expression tree (it might not be a LambdaExpression type as Expression is the abstract base class for all expression types). 虽然方法签名对我来说看起来无效,但实际上您传入的是表达式树(它可能不是LambdaExpression类型,因为Expression是所有表达式类型的抽象基类)。

The type constraints state that T must be a reference type (inherit from a class, cannot be a value type (read: struct)) and must also have a default constructor defined. 类型约束声明T必须是引用类型(从类继承,不能是值类型(读取:struct))并且还必须定义默认构造函数。

EDIT: see Jon's answer below, he corrected the signature and answered the question correctly from there, providing more information than I. 编辑:见下面Jon的回答,他更正了签名并从那里正确回答了问题,提供了比我更多的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM