Question: branch is of checkbox list type, but while looping through this it adds only one item while i want to store all "li" in branch\_id and want to retrive later why it's not adding all in branch\_is. is there any other option which can add all this to variable branch\_id. 

```
          foreach (ListItem li in branch.Items) 
                {
                    if(li.Selected)
                    {

                        List<int> branch_id = new List<int>();
                        branch_id.Add(Convert.ToInt32(li.Value));

                    }
                } 

```

Answer: 
Try this one

```
List<int> branch_id = new List<int>();
foreach (ListItem li in branch.Items) 
{
     if(li.Selected)
     {
          branch_id.Add(Convert.ToInt32(li.Value));
     }
} 

```

Or this one if you are using .Net 3.5 or higher and can use LINQ

```
List<int> branch_id = branch.Items.Where(li=>li.Selected).Select(li=>li.Value).ToList();

```
I once [wrote about an extension method](http://www.notesoncode.com/articles/2009/03/03/PowerfulExtensionMethodsPart3.aspx) that allows me to simplify the selection with LINQ:

```
var branch_id = branch.Items.WhereSelected().Select(i => Convert.ToInt32(i.Value)).ToList()

```