jonwear.com

jon wear's personal website

Object Mapping With ConvertAll

3 September 2013

Sooner or later there will come a time when you have a collection of one type of object that you want to map to another type of object. There might be a user object that contains many properties about the user but when displaying a list of users, you might only need the user name, real name and userID. I mean, <snort> why would you return the user's credit card in the JSON if you didn't really want to display it? 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.

These two classes are essentially the same, but there are a few nagging differences. Human has a LastName and a FirstName property while Person just has a Name property. Human stores height in inches while Person stores height in centimeters. Lastly, Human stores the date of birth as a string in a property called BirthDay while Person stores that information in a DateTime property called DOB. How does one convert a typed list of one kind to the other with ConvertAll? Like this: -

We could refactor this further so that we can directly map just one Person to a Human and vice versa in cases where we aren't dealing with a List. These functions allow us to pass either type to the method and get back the other type:

Now the lines of code are adding up. And if you want to run unit tests that make sure converted objects really are equal, you'll have to overload the Equals operator AND the GetHashCode method to get that to work. The more you write this code by hand, the better looking a tool like AutoMapper becomes.

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:

By Jon Wear