函数<>简单英语的运算符
我正在考虑创建一个动态 linq 查询,其中 Where
运算符将根据用户的选择而有所不同,而且我对该框架不太熟悉。看来我需要使用 Func
运算符,但我在理解这种语法时遇到了一些困难。
它有什么作用?这是什么意思?有人可以帮助我理解它是如何使用的吗?
谢谢。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Func<>
只是一个预定义的委托模式:这是一个相当标准的方法。您可以创建一个针对此签名的委托,如下所示:
此委托仅适用于采用 1 个整数并返回 bool 的方法。函数<>试图通过使其通用来简化它:
这实际上就是 Func<> 的作用。是,只是一个通用委托。这样,您就可以说出所有这些内容:
就 LINQ 而言,您可能会使用简写表达式语法;编译器会为你把表达式变成委托:
这个语句相当于:
这都是编译器语法糖。
Func<>
is simply a predefined delegate pattern:This is a fairly standard method. You can make a delegate that targets this signature like so:
This delegate is only good for methods taking 1 integer, and returning a bool. Func<> seeks to simplify this by making it generic:
That's effectively what a Func<> is, just a generic delegate. This way, you can say all of these things:
In terms of LINQ, you will likely use the shorthand expression syntax; the compiler will turn the expression into a delegate for you:
This statement is equivalent to:
It's all compiler syntax sugar.
Func
只是一个类型名称。它不是特殊的语法。您需要了解的是 lambda 表达式< /a>.例如,考虑一下:
这意味着以下内容:获取集合
myCollection
中的所有项目,检查每个项目的Size
属性是否等于10
,并且仅考虑属于这种情况的项目。还有更多使用 lambda 表达式的此类运算符。有一篇非常好的LINQ 查询简介 在 MSDN 上。
Func<T>
is just a type name. It is not a special syntax.What you need to wrap your head around are lambda expressions. For example, consider this:
This means the following: take all items in the collection
myCollection
, check if each item’sSize
property equals10
, and consider only the items for which this is the case.There are many more such operators that use lambda expressions. There is a pretty good introduction to LINQ queries on MSDN.
只需记住
Func<>
的意思是“函数”。函数基本上是一种返回结果的方法。例如:
Func
表示“一个函数返回 bool”
Func
表示“一个函数将字符串作为唯一的输入参数
并返回一个 bool"
Func
意思是“一个需要 4 个输入的函数
参数分别为
类型 bool、string、int 和 string...
返回 bool"
等等:
<>
之间列出的类型是按正确顺序排列的输入参数类型,除了最后一个是函数的返回类型。现在,到目前为止就
Where
而言,它需要Func
作为参数,这意味着任何采用TSource
输入参数的方法?并返回一个Boolean
(又名bool
)在那里就可以了(TSource
是按Where
过滤的集合中每个元素的类型)这意味着:
Where
将仅保留所选方法将返回true
的每个TSource
示例:
您想要过滤名字列表以仅保留以“T”开头的名字。
beginsWithT
是这种类型的方法:存在一种快捷方式,这使得没有必要创建这个可能只使用一次的单行方法
beginsWithT
:这意味着“对于集合中的每个元素,为了方便起见,我们将其称为
firstname
,并看看它是否以 T 开头”。此语法调用“lambda 表达式”,相当于“无名称方法”,但与beginsWithT
一样,您会注意到它需要一个string
code> 作为输入并返回一个Boolean
。因此,它是一种Func
并适合Where
的需求。希望你现在一切都清楚了! :-)
西特拉
Simply remember that
Func<>
means "function". A function is basically a method which returns a result.For example :
Func<bool>
means "a functionreturning a bool"
Func<string,bool>
means "a functiontaking a string as only input parameter
and returning a bool"
Func<bool,string,int,string,bool>
means "a function taking 4 input
parameters which are respectively of
types bool,string,int and string...
returning a bool"
And so on : the types listed between the
<>
are the input parameters types in the right order, except the last which is the return type of the function.Now, as far as
Where
is concerned, it requires aFunc<TSource, Boolean>
as argument, right ? That means that any method taking aTSource
input argument and returning aBoolean
(akabool
) will be okay there (withTSource
being the type of each element in the collection filtered byWhere
).That means :
Where
will keep only eachTSource
for which the chosen method will returntrue
.Example :
Your want to filter a list of firstnames to keep only those beginning by "T".
with
beginsWithT
being a method of this kind :a shortcut way exists, which makes unnecessary to create this one-liner method
beginsWithT
which will probably be used only one time :which means "for each element in the collection, let's call it
firstname
by convenience and let's see if it starts with T". This syntax calls a "lambda expression" and is equivalent to a "method-on-the-fly-without-name", but likebeginsWithT
you will notice that it takes astring
as input and returns aBoolean
. It is therefore a kind ofFunc<string,bool>
and fitsWhere
's needs.Hope all is clearer for you now ! :-)
Ssithra
Func
不是一个运算符 - 它只是一个通用委托类型。如果你看看它的不同“重载”的签名应该会变得更清楚:第一个是一个不带参数并返回一个值的函数,第二个是一个带一个参数并返回一个值的函数,等等在。
总而言之,它本质上是一种帮助器类型,使您能够定义函数而无需每次单独定义委托类型。
现在,这些对动态 Linq 查询都没有多大帮助,但这正是您所要求的......
Func
is not an operator - it's just a generic delegate type. If you look at the signatures of the different "overloads" of it should become clearer:The first one is a function that takes no arguments and returns a value, the second one is a function that takes one argument and returns a value, and so on.
All in all, it's essentially a helper type that enables you to define functions without separately defining a delegate type each time.
Now, none of this really helps much with dynamic Linq queries, but it is what you asked...
Func
只是一个类型名称。它不是特殊的语法。您需要了解的是表达式树< /a>.例如,考虑一下:
如果
myCollection
的类型为IQueryable
,那么编译器会将其转换为表示的表达式树 lambda 表达式item =>; item.Size == 10
。例如,如果您使用它来访问 SQL 数据库,则该表达式会自动转换为 SQL。您可以使用表达式树构建动态查询 如 MSDN 上所述。特别是,您将在
Expression
类型来动态构造这样的表达式树。Func<T>
is just a type name. It is not a special syntax.What you need to wrap your head around are expression trees. For example, consider this:
If
myCollection
is of the typeIQueryable<T>
, then the compiler will turn this into an expression tree that represents the lambda expressionitem => item.Size == 10
. If you are using this to access, for example, an SQL database, then this expression is automatically translated to SQL.You can Use Expression Trees to Build Dynamic Queries as described on MSDN. In particular, you will be using many of the static methods in the
Expression
type to construct such an expression tree dynamically.