We help IT Professionals succeed at work.

Help with IEnumerable Collection

Last Modified: 2014-05-14
Hello, I need some help understanding how to accomplish this task:

Given a class called Product with a shared method GetProducts() that returns an IEnumerable collection of Product objects with the properties ID and Desc, how would you populate a drop down list?
Watch Question

Top Expert 2015

IEnumerable is an Interface, it is not an object. All collections in .NET are IEnumerable. It is only a small subset of the collection that enables you to use it in a loop. You can always loop through the collection through IEnumerable and fill the dropdown item by item, but there is an alternative that is more interesting.

.NET collections also implement IList, which is what you are usually after when you want to fill a list of a grid with data.

Find what you have hidden under your IEnumerable collection, and use that type instead of IEnumerable. You can see what is hiding there by hitting a breakpoint when there is something in the collection. Examine the collection in the debugger, and you will have its type somewhere in there.

Using the collection by its real type instead of through one of its interfaces, you simply fill your dropdown the following way, assuming that yu are in a Windows Application. There is something similar but maybe somewhat different for Web applications.

yourDropDown.DataSource = yourCollectionObject; // This fills your dropdown with the collection
yourDropDown.DisplayMember = "Desc";

Once the user has made a selection, you can use yourDropDown.Text to retrieve the selected description, and yourDropDown.SelectedValue to get the ID.


Thank you for the explanation! So much to learn and I'm new to C#.

Would something like this work?

myProducts IList = Products.GetPlatforms();
ddlProducts.TextField = "Desc;
ddlProducts.ValueField = "ID";
ddlProducts.DataSource = myProducts;
Top Expert 2015
This one is on us!
(Get your first solution completely free - no credit card required)


Thank you for your time and help explaining. I really appreciate your help! I need to read some more.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.