472,328 Members | 1,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

Convert from List<A> to List<B>

Hi,

Have class B which subclasses from class A. I have a List<B> and I'm
trying to get a List<A> without writing a stupid loop everytime I want
to do it. So I tried this method:

public static List<Destination> ConvertList<Source,
Destination>(List<Source> sourceList) where Destination : Source
{
List<Destination> result = new List<Destination>();
foreach (Source aSource in sourceList)
{
result.Add(aSource as Destination);
}
return result;
}

It didn't work, is it possible?

Why isn't there a method like this:
List<B> aList = new List<B>();
List<A> newList = aList.Convert<A>();

Any ideas?

Thanks,

Jun 20 '06 #1
4 16907
Like so?

List<B> aList = new List<B>();
List<A> newList = aList.ConvertAll<A>(delegate(B item) {return (A) (object)
item;});

The (object) cast is needed where an explicit cast operator is not provided
by the class - or you could build the replacement objects yourself.

Marc
Jun 20 '06 #2
Additional: I didn't spot the "B subclasses A"; this means you can drop the
(object) cast

Finally - is this so you can can pass a List<A> as method parameters? If so,
note that this is a covariance issue, and can be resolved through generics
(see Right() below)

Marc

static void Main() {
List<B> aList = new List<B>();
List<A> newList = aList.ConvertAll<A>(delegate(B item) { return
(A)item; });

Wrong(aList);
Right(aList);
}
public static void Wrong(List<A> items) {
//do stuff with each A
}
public static void Right<T>(List<T> items) where T : A{
//do stuff with each T, which can also be treated as A
}
public class A { }
public class B : A { }
Jun 20 '06 #3
That looks OK. How was it failing?

bo**********@hotmail.com wrote:
Have class B which subclasses from class A. I have a List<B> and I'm
trying to get a List<A> without writing a stupid loop everytime I want
to do it. So I tried this method:
[snip]
It didn't work, is it possible?


Jun 20 '06 #4
<bo**********@hotmail.com> wrote in message
news:11**********************@b68g2000cwa.googlegr oups.com...
Hi,

Have class B which subclasses from class A. I have a List<B> and I'm
trying to get a List<A> without writing a stupid loop everytime I want
to do it. So I tried this method:

public static List<Destination> ConvertList<Source,
Destination>(List<Source> sourceList) where Destination : Source
{
List<Destination> result = new List<Destination>();
foreach (Source aSource in sourceList)
{
result.Add(aSource as Destination);
}
return result;
}

It didn't work, is it possible?


If you reverse the where constraint, this works:

public static List<Destination> ConvertList<Source,
Destination>(List<Source> sourceList)
where Source : Destination
{
List<Destination> result = new List<Destination>();

foreach (Source aSource in sourceList)
result.Add(aSource);

return result;
}

///ark
Jun 20 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Michael | last post by:
This is a two-part question to which I haven't been able to find an answer anywhere else. 1. Is it possible to format the bullet/number character...
1
by: Alex Nitulescu | last post by:
Hi. Something puzzles me: VS.NET says that "per the active schema, li cannot be nested within td". However, the code works fine, and the result is...
4
by: David Lozzi | last post by:
Howdy, I'm using a WYSIWYG editor called TinyMCE. When I edit some text and then save it back to my SQL server using a SQLCommand, all HTML...
5
by: Andrew Robinson | last post by:
Any easy answer what is wrong here? private List<string> BodyWords = new List<string>(); string word = "Andrew"; the following causes a...
7
by: mark | last post by:
Hi All, Apologies for the newbie question but I've searched and tried all sorts for a few days and I'm pulling my hair out ; Please feel free to...
5
by: David Longnecker | last post by:
I'm working to create a base framework for our organization for web and client-side applications. The framework interfaces with several of our...
2
by: -Karl | last post by:
Couls someone please advise me on this error. What I am trying to do is be able to convert an XML document into arrays. I read that the subs &...
1
by: shapper | last post by:
Hello, On my MVC projects I often create classes which contains properties which are lists of other classes. Should I start using...
2
by: Shahid | last post by:
Hi, I am parsing an .HTML file that contains following example code: <div> <p class="html_preformatted" awml:style="HTML Preformatted" dir="ltr"...
2
by: Berryl Hesh | last post by:
Converting in the other direction , IEnumerable<Interfaceto List<ImplInterface>, I can just create a new List<ImplInterface(data) where data is...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.