函数<>简单英语的运算符

发布于 2024-11-05 07:02:06 字数 179 浏览 0 评论 0 原文

我正在考虑创建一个动态 linq 查询,其中 Where 运算符将根据用户的选择而有所不同,而且我对该框架不太熟悉。看来我需要使用 Func 运算符,但我在理解这种语法时遇到了一些困难。

它有什么作用?这是什么意思?有人可以帮助我理解它是如何使用的吗?

谢谢。

I'm looking at creating a dynamic linq query where the Where operator will be different depending on the user's choice and I'm not that familiar with the framework. It seems I need to use the Func operator and I'm having some difficulty wrapping my head around this syntax.

What does it do? What does it mean? Can someone help me understand how it's used?

Thanks.

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

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

发布评论

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

评论(5

鱼忆七猫命九 2024-11-12 07:02:06

Func<> 只是一个预定义的委托模式:

protected bool SomeMethod(int input)
{
    return true;
}

这是一个相当标准的方法。您可以创建一个针对此签名的委托,如下所示:

public delegate bool MyMethodType(int input);

此委托仅适用于采用 1 个整数并返回 bool 的方法。函数<>试图通过使其通用来简化它:

public delegate T Func<T>();
public delegate U Func<T, U>(T input);

这实际上就是 Func<> 的作用。是,只是一个通用委托。这样,您就可以说出所有这些内容:

Func<int, bool> myMethod = SomeMethod;
MyMethodType myMethod = SomeMethod;

就 LINQ 而言,您可能会使用简写表达式语法;编译器会为你把表达式变成委托:

.Where(x => x.SomeProperty > 10);

这个语句相当于:

.Where(delegate(int someProperty) { return someProperty > 10) })

这都是编译器语法糖。

Func<> is simply a predefined delegate pattern:

protected bool SomeMethod(int input)
{
    return true;
}

This is a fairly standard method. You can make a delegate that targets this signature like so:

public delegate bool MyMethodType(int input);

This delegate is only good for methods taking 1 integer, and returning a bool. Func<> seeks to simplify this by making it generic:

public delegate T Func<T>();
public delegate U Func<T, U>(T input);

That's effectively what a Func<> is, just a generic delegate. This way, you can say all of these things:

Func<int, bool> myMethod = SomeMethod;
MyMethodType myMethod = SomeMethod;

In terms of LINQ, you will likely use the shorthand expression syntax; the compiler will turn the expression into a delegate for you:

.Where(x => x.SomeProperty > 10);

This statement is equivalent to:

.Where(delegate(int someProperty) { return someProperty > 10) })

It's all compiler syntax sugar.

几味少女 2024-11-12 07:02:06

Func 只是一个类型名称。它不是特殊的语法。

您需要了解的是 lambda 表达式< /a>.例如,考虑一下:

myCollection.Where(item => item.Size == 10)

这意味着以下内容:获取集合 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:

myCollection.Where(item => item.Size == 10)

This means the following: take all items in the collection myCollection, check if each item’s Size property equals 10, 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.

孤者何惧 2024-11-12 07:02:06

只需记住 Func<> 的意思是“函数”。函数基本上是一种返回结果的方法。

例如:

  • Func 表示“一个函数
    返回 bool”
  • Func 表示“一个函数
    将字符串作为唯一的输入参数
    并返回一个 bool"
  • Func
    意思是“一个需要 4 个输入的函数
    参数分别为
    类型 bool、string、int 和 string...
    返回 bool"

等等:<> 之间列出的类型是按正确顺序排列的输入参数类型,除了最后一个是函数的返回类型。

现在,到目前为止就 Where 而言,它需要 Func 作为参数,这意味着任何采用 TSource 输入参数的方法?并返回一个Boolean(又名 bool)在那里就可以了(TSource 是按 Where 过滤的集合中每个元素的类型)这

意味着:Where 将仅保留所选方法将返回 true 的每个 TSource

示例:
您想要过滤名字列表以仅保留以“T”开头的名字。

string[] firstnames = new string[] { "Albert", "Terry", "Bob", Tom", "Joe" };
string[] firstnamesBeginningWithT = firstnames.Where(beginsWithT).ToArray();

beginsWithT 是这种类型的方法:

bool beginsWithT(string firstname)
{
   return firstname.StartWith("T");
}

存在一种快捷方式,这使得没有必要创建这个可能只使用一次的单行方法 beginsWithT

string[] firstnamesBeginningWithT = firstnames.Where(firstname => firstname.StartWith("T")).ToArray();

这意味着“对于集合中的每个元素,为了方便起见,我们将其称为 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 function
    returning a bool"
  • Func<string,bool> means "a function
    taking 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 a Func<TSource, Boolean> as argument, right ? That means that any method taking a TSource input argument and returning a Boolean (aka bool) will be okay there (with TSource being the type of each element in the collection filtered by Where).

That means : Where will keep only each TSource for which the chosen method will return true.

Example :
Your want to filter a list of firstnames to keep only those beginning by "T".

string[] firstnames = new string[] { "Albert", "Terry", "Bob", Tom", "Joe" };
string[] firstnamesBeginningWithT = firstnames.Where(beginsWithT).ToArray();

with beginsWithT being a method of this kind :

bool beginsWithT(string firstname)
{
   return firstname.StartWith("T");
}

a shortcut way exists, which makes unnecessary to create this one-liner method beginsWithT which will probably be used only one time :

string[] firstnamesBeginningWithT = firstnames.Where(firstname => firstname.StartWith("T")).ToArray();

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 like beginsWithT you will notice that it takes a string as input and returns a Boolean. It is therefore a kind of Func<string,bool> and fits Where's needs.

Hope all is clearer for you now ! :-)
Ssithra

眼中杀气 2024-11-12 07:02:06

Func 不是一个运算符 - 它只是一个通用委托类型。如果你看看它的不同“重载”的签名应该会变得更清楚:

Func<TResult>
Func<T, TResult>
Func<T1, T2, TResult>

第一个是一个不带参数并返回一个值的函数,第二个是一个带一个参数并返回一个值的函数,等等在。

总而言之,它本质上是一种帮助器类型,使您能够定义函数而无需每次单独定义委托类型。

现在,这些对动态 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:

Func<TResult>
Func<T, TResult>
Func<T1, T2, TResult>

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

潇烟暮雨 2024-11-12 07:02:06

Func 只是一个类型名称。它不是特殊的语法。

您需要了解的是表达式树< /a>.例如,考虑一下:

myCollection.Where(item => item.Size == 10)

如果 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:

myCollection.Where(item => item.Size == 10)

If myCollection is of the type IQueryable<T>, then the compiler will turn this into an expression tree that represents the lambda expression item => 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.

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