You can use tools like AutoMapper (which is great) but for simple one off conversions you can make use of .ConvertAll. Keep in mind that ConvertAll is a method on the List class itself. It is not an extension method on IEnumerable.
One scenario I've come across is when two different methods return objects that describe the same data, but in slightly different ways.
Let's say you have a Person class and a Human class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Human | |
{ | |
public string LastName { get; set; } | |
public string FirstName { get; set; } | |
public string BirthDay { get; set; } | |
public Int16 HeightInInches { get; set; } | |
public override string ToString() | |
{ | |
return string.Format("{0}, {1} is {2} inches tall. Born on {3}", this.LastName, this.FirstName, this.HeightInInches, this.BirthDay); | |
} | |
} | |
public class Person | |
{ | |
public string Name { get; set; } | |
public DateTime DOB { get; set; } | |
public int HeightInCM { get; set; } | |
public override string ToString() | |
{ | |
return string.Format("{0} is {1} cm tall. Born on {2}", this.Name, this.HeightInCM, this.DOB.ToShortDateString()); | |
} | |
} |
How does one convert a typed list of one kind to the other with ConvertAll? Like this:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
List<Person> persons = PersonGenerator.Get(); | |
var humans= new List<Human>(); | |
humans = persons.ConvertAll(p => new Human | |
{ | |
FirstName = p.Name.Split(' ').FirstOrDefault() | |
, LastName = p.Name.Split(' ').LastOrDefault() | |
, BirthDay = p.DOB.ToShortDateString() | |
, HeightInInches = (short)((Double)p.HeightInCM * 0.393701) | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static dynamic PeopleConverter(dynamic sourceobject) | |
{ | |
if (sourceobject.GetType().Name=="Person") | |
{ | |
return ConvertFromPersonToHuman(sourceobject); | |
} | |
if (sourceobject.GetType().Name =="Human") | |
{ | |
return ConvertFromHumanToPerson(sourceobject); | |
} | |
throw new Exception("Bang! Pow! You submitted the wrong type!"); | |
} | |
private static Human ConvertFromPersonToHuman(Person person) | |
{ | |
Human result = new Human(); | |
string[] parts = person.Name.Split(' '); | |
result.BirthDay = person.DOB.ToShortDateString(); | |
result.FirstName = parts[0]; | |
result.LastName = parts[1]; | |
result.HeightInInches =(short)((Double) person.HeightInCM * 0.393701); | |
return result; | |
} | |
private static Person ConvertFromHumanToPerson(Human human) | |
{ | |
Person result = new Person(); | |
result.DOB = DateTime.Parse(human.BirthDay); | |
result.HeightInCM =(int)((Double)human.HeightInInches * 2.54); | |
result.Name = string.Format("{0} {1}", human.FirstName, human.LastName); | |
return result; | |
} |
So, if you are dealing with typed Lists and you only need a basic one off type conversion, it might be quicker to roll your own. You can do the whole thing in the ConvertAll statement. If the objects are more complex or you have several different mappings to worry about, you can still roll your own but, there are libraries out there to make life easier.
Source code for the entire project can be found here:
https://github.com/genghisjahn/ConvertAllExample
No comments:
Post a Comment