Reputation: 13
I have a array of 13 strings in which I pull the string values from checkboxlist. I can pull the values that are selected with the code below. However I want to also pull the unchecked values as null. So if I select 12 values then 1 would be null in the array.
I'm not sure if the array is dynamically adding the selected values or if the code fills the uncheck values with null. Please help.Thank you.
string[] selectedAreaValues = new string[13];
IEnumerable<string> allChecked = (from item in ceCheckBoxList.Items.Cast<ListItem>()
where item.Selected
select item.Value);
selectedAreaValues = allChecked.ToArray();
Upvotes: 1
Views: 233
Reputation: 4434
We may also use the alternative syntaxe + A concat between the two sequences
IEnumerable selectedItems = ceCheckBoxList.Items.Cast().Where(item => item.Selected).Select(item => new String(item.Value.toCharArray()));
Using a negation with the item.Selected boolean expression in the Where clause and using a Concat between the two sequences.
Upvotes: 0
Reputation: 70652
If you want to always return the same number of items as in the original Items
collection, but project selected items to their value and unselected items to null
, then something like this should work:
IEnumerable<string> allChecked = (from item in ceCheckBoxList.Items.Cast<ListItem>()
select item.Selected ? item.Value : (string)null);
Upvotes: 1