Solved

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

Posted on 2008-10-08
9
2,242 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
Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

 
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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

717 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