473,385 Members | 1,396 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 17086
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 of the <li>? In my styles sheet, I have the <li>...
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 as I expected. Is there something wrong in...
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 characters are changed to HTML code, i.e. &gt;strong&lt;...
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 compilation error:
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 teach me to suck eggs because it's all new to me...
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 systems and provides the business and data layer...
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 & functions have to be in <scripttags. Thanks! ...
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 IQueryable<Tor IEnumerable<Tinstead of List<T>? What are...
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" style="text-align:left"><span...
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 IEnumerable<Interface>. How can I convert the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.