Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8892313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:58:08+00:00 2026-06-14T22:58:08+00:00

While learning C# pretty fast, I am stumbling on this Collection syntax problem. I

  • 0

While learning C# pretty fast, I am stumbling on this Collection syntax problem.

I added a number of objects of my own type MyItem to listbox lstData. Now I need to search in this listbox and thought of using the elegant LINQ notation like:

lstData.Items.Where(x => x.Text == SearchString)

but the Items of a listbox does not have a .Where(), although I did include the “using System.Linq;” namespace.

So I tried:

foreach (MyItem item in (MyItem)lstData.Items)

but this gives the build error: Cannot convert type ‘System.Windows.Forms.ListBox.ObjectCollection’ to ‘MySandbox.frmListboxDemo.MyItem’.

I did manage to write something workable:

        for (int i = 0; i < lstData.Items.Count; i++)
        {
            MyItem item = (MyItem)lstData.Items[i];
            if (item.Text == SearchString)
            {
                lstData.SetSelected(i, true);
                break;
            }
        }

and a similar version like:

        var item_enum = lstData.Items.GetEnumerator();
        while (item_enum.MoveNext()) { etc etc... }

which turned out to be 2 lines longer, and without figuring what could replace ‘var’.

I am not really sure I understand how to use Collections, ObjectCollections, Enumerators etc, but am eager to learn. Especially if a .Where() version is possible and/or better.


Thanks for all your answers. I ended up with this solution:

        var item_iter = lstData.Items.Cast<MyItem>()
                          .Where(x => x.Text.Trim().ToLower() == txtItemName.Text);
        foreach (MyItem item in item_iter)
        {
            int i = lstData.Items.IndexOf(item);
            lstData.SetSelected(i, true);
            break;
        }

I don’t know if this is really much better than the “fortran” way (see above), but it does teach me methods I can use in other C# queries.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T22:58:09+00:00Added an answer on June 14, 2026 at 10:58 pm

    Try using the Cast<T> first to perform a cast to the type you expect it to be. That is:

    lstData.Items.Cast<MyItem>().Where(x => x.Text == SearchString)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry since this question is specific to my problem. While learning reflections i did
While learning Rails, I found this example in the Sam Ruby's book: app/controllers/line_items_controller.rb def
HI there! I've run into some problem while learning to combine .sh files and
I'm pretty new to php but i'm learning pretty fast, I have a question
I need to shorten this query and while I'm pretty good at SQL, I'm
While learning an example from learn-you-a-haskell which right triangle that has integers for all
I am developing a small app while learning Android. The app is basically making
i just got some more questions while learning PHP, does php implement any built
MOV is probably the first instruction everyone learns while learning ASM. Just now I
I've slightly modified the iPhone SDK's GLSprite example while learning OpenGL ES and it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.