了解Lambda表达式的工作原理 [英] Understanding how lambda expression works

查看:96
本文介绍了了解Lambda表达式的工作原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Regex regex = new Regex("^[a-zA-Z0-9,-_ ]{2,12}$");
            return panel1.Controls.OfType<UserControl1>()
.Select(uc => uc.comboBox2).Any(cb => cb.Text == String.Empty);

因为代码是用于验证的,所以我正在实现它,但是我不知道它是如何工作的以及它在哪里流动.谁能帮我理解这行吗?

as the code is for validation I am implementing this but I don't know how it works and where it flows. Can anyone help me to understand this line ?

return panel1.Controls.OfType<UserControl1>()
.Select(uc => uc.comboBox2).Any(cb => cb.Text == String.Empty);

推荐答案

panel1-表单上的控件
.controls-添加到panel1
的控件列表 .OfType<UserControl1>()-仅类型为UserControl1
的控件 .Select(uc => uc.combobox2)-(每个UserControl1(在panel1中的)每个combobox2的)属性
.Any(cb => cb.Text == String.Empty)-如果这些comboxbox的Text属性中的任何一个为空字符串,则计算结果为true.

panel1 - the control on your form
.controls - the list of controls added to panel1
.OfType<UserControl1>() - only controls that are of type UserControl1
.Select(uc => uc.combobox2) - the combobox2 property (of each UserControl1 (in panel1))
.Any(cb => cb.Text == String.Empty) - evaluates to true if any of those comboxbox's Text property is an empty string.

因此,基本上,如果添加到panel1控件的UserControl1combobox2属性的Text属性中的任何一个为空,则返回true,否则返回false.

So basically if any of the Text property of the combobox2 property of the UserControl1's added to your panel1 control is empty, then return true, otherwise false.

关于.Select(uc => uc.comboxbox2)的一些解释.

这是说调用集合uc中的每个项目.对于每个uc,返回在=>右侧创建的值.在这种情况下,它是uc.combobox2.想象一下,对单个对象执行此操作,那么您会得到一个ComboBox类型的变量(我猜是这样).因为这是在Select方法的上下文中,所以我们将为每个项目执行此操作.对所有对象执行此操作意味着您将基于UserControl1的集合来获取它们的集合.

This is saying call each item in the collection uc. For each uc return the value created on the right of the =>. In that case it's uc.combobox2. Imagine doing that to a single one, well you'd get a variable of type ComboBox (i'd guess). Because this is in the context of the Select method, we will do that for each item. Doing it for all of them means you get a collection of them, based on your collection of UserControl1's.

关于.Any(cb => cb.Text == String.Empty) Any方法的参数与上述相同,但是由于在=>右侧"的部分在这种情况下的计算结果为truefalse,因此可能会造成混淆. Any方法期望将集合中的每个项目(在本例中为ComboBox)转换为true或false的东西.如果其中任何一个转换为true,它将返回true,否则将返回false.

Regarding .Any(cb => cb.Text == String.Empty) The parameter to the Any method is the same thing as above, however it might be confusing as the part "on the right of the =>" is in this case something that evaluates to true or false. The Any method expects something that transforms each item in a collection (a ComboBox in this case) into a true or false. It will then return true if any of those transform into true, and if not then it'll return false.

这篇关于了解Lambda表达式的工作原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆