简体   繁体   English

有人可以解释一下 <Func<T, bool> &gt;对我来说很简单

[英]Can someone please explain the <Func<T, bool>> in a simple way for me

I am looking at code containing: 我正在查看包含的代码:

public virtual ICollection<T> GetPk(string pk)
{
    Expression<Func<T, bool>> predicate = c => c.PartitionKey == pk;
    return this.GetAll(predicate);
}

Can someone explain the syntax of <Func<T, bool>> ? 有人可以解释<Func<T, bool>>的语法吗?

One of the best explanation can be found at MSDN 最好的解释之一可以在MSDN上找到

You can use this delegate to represent a method that can be passed as a parameter without explicitly declaring a custom delegate. 您可以使用此委托来表示可以作为参数传递的方法,而无需显式声明自定义委托。 The encapsulated method must correspond to the method signature that is defined by this delegate. 封装的方法必须对应于此委托定义的方法签名。 This means that the encapsulated method must have one parameter that is passed to it by value, and that it must return a value. 这意味着封装的方法必须具有一个按值传递给它的参数,并且它必须返回一个值。

As for argument in your example T is the type of input parameter and bool is the return type of exacted method. 至于示例中的参数,T是输入参数的类型,而bool是精确方法的返回类型。

A Func<T, bool> represents a function that takes an object of type T and returns a bool . Func<T, bool>表示一个函数,该函数采用类型T的对象并返回bool It's commonly referred to as a "predicate", and is used to verify a condition on an object. 通常称为“谓词”,用于验证对象上的条件。

An Expression<Func<T, bool>> represents the abstract syntax tree of the function, ie its syntactic structure. Expression<Func<T, bool>>表示该函数的抽象语法树 ,即其语法结构。 It can be used to analyse the code of the function for various purposes, such as transforming it to SQL to be executed against a database. 它可用于出于各种目的分析功能的代码,例如将其转换为要针对数据库执行的SQL。

Simply Func<T, bool> is the anonymous method signature. 仅仅是Func<T, bool>是匿名方法签名。 The first type T is the input parameter type and the second type is the return type. 第一种类型T是输入参数类型,第二种类型是返回类型。 This is more like a method when you consider your representation: 考虑表示形式时,这更像是一种方法:

bool AnonMethod(T arg0)
{
   return arg0.PartitionKey == pk;
}

I always find MSDN to be worth checking on things like this first, 我总是觉得MSDN首先值得对这种事情进行检查,

http://msdn.microsoft.com/en-us/library/bb549151.aspx http://msdn.microsoft.com/zh-CN/library/bb549151.aspx

Beaten by Maheep, didn't see the post message pop-in. 被Maheep打败,没有看到该帖子的弹出窗口。

Basically, you're declaring a method that matches a signature, that can then be passed in to the call to get the data. 基本上,您要声明一个与签名匹配的方法,然后可以将其传递给调用以获取数据。

It is confusing at first but Func<T, bool> describes a function that returns a bool and accepts a parameter as type T. 起初令人困惑,但是Func<T, bool>描述了一个返回bool并接受参数作为类型T的函数。

In this case, T is an object that has a PartitionKey property and this GetPk method is using the Func<T, bool> to match all the T items in the instance object which have a PartitionKey that matches the string pk . 在这种情况下,T是一个具有PartitionKey属性的对象,并且此GetPk方法使用Func<T, bool>来匹配实例对象中所有具有与字符串pk匹配的PartitionKey的T项目。

For some background; 对于某些背景; prior to Func<T, TResult> (and the rest of this family) being part of the framework, you either had to explicitly define delegates or use anonymous methods. Func<T, TResult> (以及该系列的其余部分)成为框架的一部分之前,您必须明确定义委托或使用匿名方法。

Func and Action were added as part of the addition of lambda expressions to the language. FuncAction被添加为该语言中lambda表达式的一部分。 They are the framework-defined delegates which lambda expressions are typed as, but which you as a developer can also use in place of your own custom delegate definitions. 它们是框架定义的委托,lambda表达式被键入为该委托,但是作为开发人员,您也可以使用它们代替您自己的自定义委托定义。

You can get a nice history here; 您可以在这里获得良好的历史记录;

http://blogs.msdn.com/b/ericwhite/archive/2006/10/03/lambda-expressions.aspx http://blogs.msdn.com/b/ericwhite/archive/2006/10/03/lambda-expressions.aspx

It's additional syntax so you know what goes in and out of the function. 它是附加的语法,因此您知道该函数有哪些内容。

Func<T, bool> means: function has 1 input T and 1 output that's bool . Func<T, bool>意思是:函数有1个输入 T1个输出bool

This is other variations of the function 这是功能的其他变化

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

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