karakav
asked on
C#:Convert IList<object> to List<object>
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(.....);
ASKER
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.
use var1.ToList<> method.
ASKER
I can't. I am using C# 2.0.
IList<object> var1 = .....;
List<object> var2 = new List<object>(var1);
List<object> var2 = new List<object>(var1);
why doesnt it work karakav, what exception is raised? It should work.
You need to provide some info in order to be helped.
You need to provide some info in order to be helped.
ASKER
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.
did it work ?
IList<object> var1 = .....;
List<object> var2 = new List<object>(var1);
IList<object> var1 = .....;
List<object> var2 = new List<object>(var1);
ASKER
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.
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.
So, you dont get compilation errors, only runtime exception? And Entity positively implements IEntity?
ASKER
As I said before, I get an InvalidCastException, which means I get it at runtime.
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.
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>;
ASKER
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>?
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.
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This is actually what I am using but I wanted another approach that doesn't require iterations.
:)
try this, altough I dont know how much effective is your approach
List<Entity> list = new List<Entity>((Entity[]) theList.ToArray( typeof(Entity)));
try this, altough I dont know how much effective is your approach
List<Entity> list = new List<Entity>((Entity[]) theList.ToArray( typeof(Entity)));
Btw, I have assumed that collection theList is of type ArrayList.
ArrayList theList = ...// query from iBatis
ASKER
What do you mean about the effectiveness of the approach?
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.
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.
ASKER
For the time being the iteration works fine, so I will stick to that.
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.
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