Solved

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

Posted on 2008-10-08
9
2,196 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 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