Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

C#: Convert IEnumerable<Object> to a List<Object>

Posted on 2008-10-08
9
Medium Priority
?
2,282 Views
Last Modified: 2013-12-17
Can some one help me conert an IEnumerable of objects to a List of the same type. I have a method that returns an IEnumerable of objects to take advantage of the yield return but on the other hand I need to objects to be in a generic typed List. Some idea please?

P.S: I am using .Net Framework 2.0, so please don't bring Linq in here.
0
Comment
Question by:karakav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22667721
can you share some code snippet?
0
 
LVL 4

Author Comment

by:karakav
ID: 22667901
Here you are. Actually I need List<> to take advantage of its Find method.
        private IEnumerable<Entity> GetEntities()
        {
            foreach (Entity e1 in Collection)
            {
                if (someCondition)
                {
                    yield return e1;
                }
            }
        }
 
 
List<Entity> theList = GetEntities();//I need the conversion here
Entity e2 = theList.Find(delegate(Entity eOther)
{
return eOther == somevalue;
});

Open in new window

0
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22667933
ok. so basically you need to create a predicate.. check the link below if it helps:
http://msdn.microsoft.com/en-us/library/fh1w7y8z.aspx
 
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 14

Expert Comment

by:Ramuncikas
ID: 22667938
List implements IEnumerable so this should work.



List<Entity> theList = (List<Entity>)GetEntities();

Open in new window

0
 
LVL 4

Author Comment

by:karakav
ID: 22668187
The link you show is just explaining how to find item in the  List, an information I already have(Check the snippet I sent before).

And the explicit casting doesn't work neigher.
0
 
LVL 14

Accepted Solution

by:
Ramuncikas earned 2000 total points
ID: 22668427

List<Entity> theList = new List<Entity>(GetEntities());

Open in new window

0
 
LVL 13

Expert Comment

by:SameerJagdale
ID: 22668488
it did work for me.. check the way you created the predicate.. :-)
0
 
LVL 4

Author Comment

by:karakav
ID: 22668531
Maybe you can send a snippet so that I compare with my code.
0
 
LVL 4

Author Closing Comment

by:karakav
ID: 31504164
Thanks.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question