从复选框列表中获取所有值

Getting all values from Checkboxlist

我有一个包含 13 个字符串的数组,我从复选框列表中提取字符串值。我可以使用下面的代码提取 selected 的值。但是我也想将未检查的值拉为空。因此,如果我 select 12 个值,则数组中的 1 将为空。

我不确定数组是否正在动态添加 selected 值,或者代码是否用 null 填充未选中的值。请help.Thank你

string[] selectedAreaValues = new string[13];

IEnumerable<string> allChecked = (from item in ceCheckBoxList.Items.Cast<ListItem>()
                                  where item.Selected
                                  select item.Value);

selectedAreaValues = allChecked.ToArray();

如果您想要始终 return 与原始 Items 集合中相同数量的项目,但将选定项目投影到它们的值并将未选定项目投影到 null,那么一些这样应该可以工作:

IEnumerable<string> allChecked = (from item in ceCheckBoxList.Items.Cast<ListItem>()
                                  select item.Selected ? item.Value : (string)null);

我们也可以使用替代语法 + 两个序列之间的连接

  1. IEnumerable selectedItems = ceCheckBoxList.Items.Cast().Where(item => item.Selected).Select(item => 新字符串(item.Value.toCharArray()));

  2. 在 Where 子句中对 item.Selected 布尔表达式使用否定,并在两个序列之间使用 Concat。