Solved

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

Posted on 2008-10-08
9
2,165 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
  • 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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 500 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now