0

At the moment I have to break down this simple operation in two parts, I am sure the would be a better way is hiding from me :

List<int> selectedValues= new List<int>();
...
IEnumerable<RadComboBoxItem> checkedItems = from checkedItem in cblMagistrateCourts.Items.ToList()
                                             where checkedItem.Checked == true
                                             select checkedItem;
foreach (RadComboBoxItem item in checkedItems)
{
     if (item.Checked)
          selectedValues.Add(Convert.ToInt32(item.Value));
}

I am wanting this to be done server-side only.

icebat
  • 4,696
  • 4
  • 22
  • 36
ablaze
  • 722
  • 7
  • 30
  • 1
    What do you mean with server-side only? *UserControls* are scarcely supported server-side. Which code part should be executed server-side? And which technology is used? WPF, Silverlight, ASP.NET? Does this come along with any MVVM framework? We need a bit more information about your concern. – Michael Schnerring Feb 27 '13 at 12:37
  • Dear @ebeeb, certainly you're missing the fact that I can do the same on client side too, using JavaScript and I already was performing it, but turned out the I was getting the values after the page had loadded and it misguided the WCF service I was using from within JavaScript. But thanks though ! – ablaze May 13 '13 at 17:54

3 Answers3

4

How about this?

List<int> selectedValues = cblMagistrateCourts.Items.Where(i => i.Checked)
                                                    .Select(i => Convert.ToInt32(i.Value))
                                                    .ToList();
icebat
  • 4,696
  • 4
  • 22
  • 36
  • Or perhaps `.Select(i => int.Parse(i.Value.ToString()))` to include values like "3" – Jens Kloster Feb 27 '13 at 12:21
  • @JensKloster, I agree about converting. Just added `Convert` in code. It\`s better, than converting to `string`, IMO, and he already uses it. – icebat Feb 27 '13 at 12:24
  • I think the OP asks for how to redesign his code. You still operate on the `RadComboBox` (`cblMagistrateCourts`), which won't be provided *server-side*. – Michael Schnerring Feb 27 '13 at 12:33
  • @ebeeb, we`ll have to wait for comments from OP then, because I don\`t see anything server-side here. He just gets values from CB items (no data binding, I guess?). – icebat Feb 27 '13 at 12:37
1

Convert the value at same time you're selecting the checkedItem...

List<int> selectedValues = (from checkedItem in cblMagistrateCourts.Items.ToList()
                            where checkedItem.Checked == true
                            select Convert.ToInt32(checkedItem.Value)).ToList();
sesispla
  • 300
  • 1
  • 4
  • Thansk @NeWNeO but turns out, Where(i => i.Checked) from icebat's solution it more efficient as it narrows down the sequence itself. Other than that yours is too close to exactly what I was looking for. :-) – ablaze May 13 '13 at 17:51
-1
rcb.CheckedItems.Select(x => x.Value).ToList();
Ali
  • 9
  • 1
  • 3