LINQ:使用一个lambda前pression获取的CheckBoxList的所有选择值 [英] LINQ: Get all selected values of a CheckBoxList using a Lambda expression

查看:226
本文介绍了LINQ:使用一个lambda前pression获取的CheckBoxList的所有选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要检索列表的IEnumerable 所有选中的复选框中的值的考虑的方案一个< ASP:&的CheckBoxList GT;

Consider a scenario where you want to retrieve a List or IEnumerable of the values of all the selected checkboxes in an <asp:CheckBoxList>.

下面是当前实现:

IEnumerable<int> allChecked = (from item in chkBoxList.Items.Cast<ListItem>() 
                               where item.Selected 
                               select int.Parse(item.Value));

:你会如何使用拉姆达前pression或lambda语法改进这个LINQ查询

Question: How would you improve this LINQ query using a lambda expression or lambda syntax?

推荐答案

您的的使用lambda前pressions - 他们只是被你使用C#的查询操作的隐蔽

You are using lambda expressions - they are just concealed by your use of C#'s query operators.

考虑这个:

IEnumerable<int> allChecked = (from item in chkBoxList.Items.Cast<ListItem>() 
                               where item.Selected 
                               select int.Parse(item.Value));

被编译到这一点:

Gets compiled to this:

IEnumerable<int> allChecked = chkBoxList.Items.Cast<ListItem>()
                              .Where(i => i.Selected)
                              .Select(i => int.Parse(i.Value));

你可以,你已经在使用两个拉姆达前pressions看到(它们的参数的其中,选择方法),你甚至不知道它!这种查询是很好,我不会改变它。

As you can see you are already using two lambda expressions (they are the parameters to the Where and Select methods) and you didn't even know it! This query is fine and I wouldn't change it at all.

这篇关于LINQ:使用一个lambda前pression获取的CheckBoxList的所有选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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