表达式> 的作用是什么?声明是什么意思?

发布于 2024-12-28 08:04:24 字数 352 浏览 4 评论 0 原文

有人可以以传达表达式含义以及如何调用它的方式解释以下声明吗?

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

我将其读为: 通过传入参数为返回 boolT 类型对象的 lambda 表达式来删除 T 类型的对象。

另外,可以更换吗 Func;表达式谓词;表达式

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.

Also, can you replace
Func<T, bool> expression
with
Predicate<T> expression

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薄荷→糖丶微凉 2025-01-04 08:04:24

这个方法可能是集合类型的成员,是吗?

“谓词”是对“这个东西是该集合的成员吗?”这个问题说“是”或“否”的任何手段。因此,集合“整数甚至正整数”的谓词将是 x=> x> 0 && x%2==0。

该方法可能具有“从集合中删除由谓词标识的集合中的所有集合成员”的语义。

谓词以表达式树的形式传递给方法,这是一种以可在运行时分析和转换的方式传递谓词结构的方式。它通常用于这样的场景:“集合”实际上是某个地方的数据库,并且删除请求需要翻译成数据库的查询语言的查询并通过网络发送。

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.

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.

落在眉间の轻吻 2025-01-04 08:04:24

第一个是接受表达式树(不一定是从 lambda 表达式树创建)的方法。表达式树表示接受 T 并返回 bool 的表达式。 T 被限制为具有无参数构造函数的引用类型。

至于语义 - 这取决于文档/实现。

区分 lambda 表达式(创建表达式树的一种方法)和表达式树本身非常重要。

至于是否可以使用 Predicate 代替 - 也许吧。这取决于实现如何处理它。当然,它们代表相同的委托签名 - 但您不能在两种类型的表达式树之间简单地进行转换。

The first is a method which accepts an expression tree (not necessarily created from a lambda expression tree). The expression tree represents an expression which accepts a T and returns a bool. T is constrained to be a reference type with a parameterless constructor.

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.

As for whether it could use Predicate<T> instead - maybe. 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.

‘画卷フ 2025-01-04 08:04:24

此方法获取函数的参数表达式树,该函数使用公共无参数构造函数获取对象并返回布尔值。

您可以在此处阅读有关表达式树及其用法的更多信息:
http://msdn.microsoft.com/en-us/library/bb397951.aspx

this methods gets as a parameter expression tree of function that gets object with public parameter-less constructor and returns boolean.

you can read more about expression trees and their usage here:
http://msdn.microsoft.com/en-us/library/bb397951.aspx

浊酒尽余欢 2025-01-04 08:04:24

虽然方法签名对我来说看起来无效,但本质上您正在传递一个表达式树(它可能不是 LambdaExpression 类型,因为 Expression 是所有表达式类型的抽象基类)。

类型约束规定 T 必须是引用类型(从类继承,不能是值类型(读:结构)),并且还必须定义一个默认构造函数。

编辑:请参阅下面乔恩的答案,他更正了签名并从那里正确回答了问题,提供了比我更多的信息。

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).

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.

EDIT: see Jon's answer below, he corrected the signature and answered the question correctly from there, providing more information than I.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文