Click here to Skip to main content
Licence CPOL
First Posted 19 Apr 2008
Views 11,923
Bookmarked 11 times

IEnumerable Object Converter - Extension Method

By | 19 Apr 2008 | Article
Collection Object Converter

Introduction

This is a useful extension method for collections to convert an object collection in a single line of code.

Background

The goal of this example is to bring a developer an easy way to convert an object collection between different object types.

Using the Code

Add the convertor method to your project.

Using one line of code, you can convert one object collection type to another.

Instantiate the delegate using an anonymous method inline:

public static IEnumerable<TConvertedObject> Convertor<TObjectToConvert, TConvertedObject>
          (this IEnumerable<TObjectToConvert> collection, Func<TObjectToConvert
        , TConvertedObject> converter)
        {
                return new List<TObjectToConvert>(collection)
                    .ConvertAll<TConvertedObject>
                    (delegate(TObjectToConvert from)
                    {
                            return converter(from);

                    });
        }

Example

public class Person
{
    public long Id { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
}

public class PersonItem
{
    public long Id { get; set; }
    public string Name { get; set; }
}

IList<Person> personCollection = new List<Person>();
personCollection.Add(new Person { Id = 1, Name = "shay", Address = "isr" });
personCollection.Add(new Person { Id = 2, Name = "pap", Address = "isr" });
//Implement object converter
Func<Person,PersonItem> convert = (p) => new PersonItem{ Id = p.Id, Name = p.Name };

IEnumerable<PersonItem> personItemCollection = 
    personCollection.Convertor<Person, PersonItem>(convert);

Points of Interest

Wrapping up the List ConvertAll method.

History

  • 19th April, 2008: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

shaykeren

Software Developer
QualiSystems
Israel Israel

Member

shaykeren.blogspot.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralOr.... Pinmemberjaybuffet3:40 16 Jul '08  
GeneralSuggestions PinmemberMustafa Ismail Mustafa2:38 19 Apr '08  
GeneralRe: Suggestions Pinmembershaykeren6:30 19 Apr '08  
GeneralRe: Suggestions PinmemberMustafa Ismail Mustafa9:40 19 Apr '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 19 Apr 2008
Article Copyright 2008 by shaykeren
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid