question

TZacks-2728 avatar image
0 Votes"
TZacks-2728 asked vb2ae answered

How to use LINQ to select data from combobox with where filter

I am working with winform. I found this code which throwing error The type or namespace name 'ComboBoxItem' could not be found (are you missing a using directive or an assembly reference?)

 var selectedperiods = cboPeriods.Items.Cast<ComboBoxItem>().Where(i => (int.Parse(i.Value.ToString())) == 1);


i am getting error for ComboBoxItem. tell me how to sort it ?. i have to use LINQ to extract data from ComboBox with where filter. thanks



dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

vb2ae avatar image
0 Votes"
vb2ae answered

Assuming this is for a windows forms combobox. You could try casting it to the object type in the combobox. I assume you added integers like this to the combobox.

         comboBox1.Items.Add(1);
         comboBox1.Items.Add(2);
         comboBox1.Items.Add(3);
         comboBox1.Items.Add(4);
         comboBox1.Items.Add(5);
         comboBox1.Items.Add(6);
         comboBox1.Items.Add(7);
         comboBox1.Items.Add(8);
         this.Load += Form1_Load;
     }

     private void Form1_Load(object sender, EventArgs e)
     {
         var itm = comboBox1.Items.Cast<int>().Where(i => i == 1);
     }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.