LINQ: Get all selected values of a CheckBoxList using a Lambda expression
asp.net, c#, linq, webforms
Solution
You are using lambda expressions - they are just concealed by your use of C#'s query operators.
Consider that this:
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));
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.
Problem
Consider a scenario where you want to retrieve a `List` or `IEnumerable` of the values of all the selected checkboxes in an `<asp:CheckBoxList>`. Here's the current implementation: ``` IEnumerable<int> allChecked = (from item in chkBoxList.Items.Cast<ListItem>() where item.Selected select int.Parse(item.Value)); ``` Question: How would you improve this LINQ query using a lambda expression or lambda syntax?