c#: ConvertAll with Lists

3 comments

Convert all members in a List from one entity to another.

I have posted before about ConvertAll, but there I used an anonymous method within ConvertAll.

I wanted to use a normal mapping-method that I could reuse, but still call it with ConvertAll.

I had to use some time to get the syntax right, and that's what this post is about.

Here is the syntax I ended up with:

                                              

                                              

List<KorrespondanseParameter> newlist = mylist.ConvertAll<KorrespondanseParameter>(new Converter<IKorrespondanseParameter,KorrespondanseParameter>(Map));

And the converter:

private static KorrespondanseParameter Map(IKorrespondanseParameter k)     

{

            KorrespondanseParameter r = new KorrespondanseParameter();

            r.GjelderAktoerId = k.GjelderAktoerId;

            r.Mottatt = k.Mottatt;

            r.Oppgave = k.Oppgave;

            r.Saksnummer = k.Saksnummer;

            r.Sendt = k.Sendt;

            return r;

}

As we can see the implementation is straightforward, nothing complex there as long as you know how to call it.

Still took me some extra time to get this right.

Rgds

HM

Posted via email from Henris blogg

3 comments :