Solved

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

Posted on 2008-10-08
9
2,215 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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