• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2327
  • Last Modified:

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

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
karakav
Asked:
karakav
  • 4
  • 3
  • 2
1 Solution
 
SameerJagdaleCommented:
can you share some code snippet?
0
 
karakavAuthor Commented:
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
 
SameerJagdaleCommented:
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
Independent Software Vendors: 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!

 
RamuncikasCommented:
List implements IEnumerable so this should work.



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

Open in new window

0
 
karakavAuthor Commented:
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
 
RamuncikasCommented:

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

Open in new window

0
 
SameerJagdaleCommented:
it did work for me.. check the way you created the predicate.. :-)
0
 
karakavAuthor Commented:
Maybe you can send a snippet so that I compare with my code.
0
 
karakavAuthor Commented:
Thanks.
0

Featured Post

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.

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now