Reputation: 51241
TEntity Single(Expression<Func<TEntity, bool>> predicate);
Please explain the parameter.
Upvotes: 3
Views: 145
Reputation: 15579
This is a expression used to specify any delegate function taking any "TEntity" (as defined in this instance by the collection you are calling it on) and returning a bool. In practice, the delegate is specified using a lambda function:
items.Single(i => i.Id == 1);
In your example, the Single function is an extension method that is applied to a generic collection of TEntity (which I believe don't have any limitations - i.e. must just be objects). Therefore, the type of TEntity is inferred based on the collection that you are calling it on.
Upvotes: 0
Reputation: 48137
So, there is a lot going on here, but lets start with the inside:
Func<TEntity, bool>
is a delegate that takes an input, which type is generic, so we just call it TEntity
. Without any contraints, this can be anything, but a strongly-typed anything.
Out one level is Expression<Func<TEntity, bool>>
. This is the expression tree, which is strongly typed to be a delegate that takes an input and return a bool. In other words, it is an expression tree (think back to your compilers course) that represents the function.
Finally, the outermost level: TEntity Single(Expression<Func<TEntity, bool>> predicate)
is a method called Single
which takes a predicate function in the form of an expression. What is returned is the same type that is passed into that function.
Essentially, Single
will take your expression, compile it and execute it against a set of data, returning the first entity in the collection that matches the predicate expression.
Hope this helps?
Upvotes: 8