What does the Expression<Func<T,bool>> declaration mean?
Asked Answered
Y

4

14

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

Yellowtail answered 20/1, 2012 at 14:19 Comment(2)
i think you have made a mistake during transposing your method signature from your IDE to the page, there is no mention of TKind
You're right, my markdown was wrong.Yellowtail
P
20

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.

Plotinus answered 20/1, 2012 at 15:5 Comment(2)
Yes, Delete here operates on a collection to be persisted to a database. I like the semantics you use.Yellowtail
@EricLippert: How can it be called/Hornet
O
9

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.

Officialdom answered 20/1, 2012 at 14:23 Comment(1)
Thanks for correcting the signature in the posted code Jon :)Kind
M
1

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

Monodic answered 20/1, 2012 at 14:26 Comment(0)
K
0

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.

Kind answered 20/1, 2012 at 14:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.