[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

C#:Convert IList<object> to List<object>

Posted on 2008-10-10
21
Medium Priority
?
18,227 Views
Last Modified: 2013-12-17
Is there a way I can convert IList<object> to List<object>? I tried explicit conversion but I failed.
IList<object> var1 = .....
((List<object)var1).Find(.....);

Open in new window

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
  • 9
  • 8
  • 3
  • +1
21 Comments
 
LVL 18

Expert Comment

by:Priest04
ID: 22685380
ILIst<T> coll1 = GetData();
List<T> coll2 = (List<T>)coll1;

or you can do it in single line, when getting data;

List<T> coll2 = GetData() as List<T>; // where GetData returns IList<T>

Goran
0
 
LVL 4

Author Comment

by:karakav
ID: 22685454
Sorry, I made a Mystake in my question. I would like to convert IList<IEntity> to List<Entity>. Please note that it is a collection of interfaces.
0
 
LVL 7

Expert Comment

by:ajolly
ID: 22685489
use var1.ToList<> method.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 4

Author Comment

by:karakav
ID: 22685518
I can't. I am using C# 2.0.
0
 
LVL 7

Expert Comment

by:ajolly
ID: 22685607
IList<object> var1 = .....;
List<object> var2 = new List<object>(var1);
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22685631
why doesnt it work karakav, what exception is raised? It should work.

You need to provide some info in order to be helped.
0
 
LVL 4

Author Comment

by:karakav
ID: 22685813
Sorry, I thought my question was clear. Let me update again. When I use explicit cast I get an InvalidCastException. When I cast using the cast keyword, i just get a null collection.
0
 
LVL 7

Expert Comment

by:ajolly
ID: 22685829
did it work ?
IList<object> var1 = .....;
List<object> var2 = new List<object>(var1);
0
 
LVL 4

Author Comment

by:karakav
ID: 22685837
Dear ajolly,

You solution doesn't work because I use a collection of interfaces. And frankly speaking, I don't understand why this should not work because it works when I replace the interface with the object that inherit that interface.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22686331
So, you dont get compilation errors, only runtime exception? And Entity positively implements IEntity?
0
 
LVL 4

Author Comment

by:karakav
ID: 22709170
As I said before, I get an InvalidCastException, which means I get it at runtime.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22709483
You said it before, but it doesnt makes sence. I have just created a sample that uses IEntity and Entity, just to confirm that it is possible.

So, the answer to your question is: yes, it is possible, and is straightforward, as you can see the code bellow.
interface IEntity
{
    string PropertyA{get;set;}
    string PropertyB {get;set;}
    string MethodA();
}
 
class Entity : IEntity
{
    string _PropertyA;
    string _PropertyB;
 
    public Entity(string propertyA, string propertyB)
    {
        _PropertyA = propertyA;
        _PropertyB = PropertyA;
    }
 
    #region IEntity Members
 
    public string PropertyA
    {
        get
        {
            return _PropertyA;
        }
        set
        {
            _PropertyA = value; ;
        }
    }
 
    public string PropertyB
    {
        get
        {
            return _PropertyB;
        }
        set
        {
            _PropertyB = value;
        }
    }
 
    public string MethodA()
    {
        return PropertyA + " " + PropertyB;
    }
 
    #endregion
 
    public bool PropertyC
    {
        get { return true; }
    }
 
    public void MethodB()
    {
            
    }
}
 
IList<IEntity> list = new List<IEntity>();
 
list.Add((IEntity)(new Entity("a", "b")));
list.Add((IEntity)(new Entity("c", "d")));
list.Add((IEntity)(new Entity("e", "f")));
 
List<Entity> list2 = list as List<Entity>;

Open in new window

0
 
LVL 4

Author Comment

by:karakav
ID: 22718714
Sorry Priest04. I reall am sorry. I just found out that actually the object I have is a IList that has IEntity object inside . I get it from a iBatis query and for a reason I don't know, it has to come out this way. I can't declare IList<IEntity> and then execute the query and I will get a conversion error. But suprisingly I cant loop through the list in the snippet.

My problem still however, how do I convert that IList in a List<Entity>?
IList theList = ...// query from iBatis
foreach(IEntity in theList)
{
//Do processing here.
}

Open in new window

0
 
LVL 18

Accepted Solution

by:
Priest04 earned 2000 total points
ID: 22723389
Have you tried this?


IList theList = ...// query from iBatis
List<Entity> list = new List<Entity>();
 
foreach(object obj in theList)
{
    list.Add((Entity)obj);
}

Open in new window

0
 
LVL 4

Author Comment

by:karakav
ID: 22723456
This is actually what I am using but I wanted another approach that doesn't require iterations.
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22724152
:)

try this, altough I dont know how much effective is your approach

List<Entity> list = new List<Entity>((Entity[]) theList.ToArray( typeof(Entity)));
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22724816
Btw, I have assumed that collection theList is of type ArrayList.

ArrayList theList = ...// query from iBatis

Open in new window

0
 
LVL 4

Author Comment

by:karakav
ID: 22728441
What do you mean about  the effectiveness of the approach?
0
 
LVL 18

Expert Comment

by:Priest04
ID: 22729002
I am not familiar with the internal mechanism of the ToArray method, but could be that the code is literating twice, so the performance can drop. Test it to see which one runs faster, and use the faster onr, not the "better looking" one.

btw

>> This is actually what I am using but I wanted another approach that doesn't require iterations.

All methods reqire iterations, its just weather you will do it in the code, or they will provide a method that does an iteration.

And there is no other way to achieve what you want.
0
 
LVL 4

Author Closing Comment

by:karakav
ID: 31504940
For the time being the iteration works fine, so I will stick to that.
0
 

Expert Comment

by:Justice777
ID: 25699158
What you are talking about is Covariance. It is handled in .net 4.0, but unfortunately, 2.0 does not, so iteration from one list to the other is the only means I know of.
0

Featured Post

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!

Question has a verified solution.

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

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.…
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:…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

656 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