I see many developers convert one list to another list using foreach. It is ok and get the job done but there is better way to convert one list to another list using ConvertAll();

Just to explain you scenario better, let us assume you got two classes as below. InternalStudent class is representing Student class internally in the project and this is not supposed to be exposed in Service etc.


public class InternalStudent
    {
        public string IName { get; set; }
        public int IAge { get; set; }
    }

Another class is Student


  public class Student
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

Now you have List of InternalStudents as below,


List<InternalStudent> lstInterStudent = new List<InternalStudent>
            {
                new InternalStudent {IName="dj",IAge=32},
                new InternalStudent{IName="pinal",IAge=36}
            };

Requirement is to convert List<InternalStudent> to List<Student>. Usually we convert one list to another list using foreach as given below,


List<Student> lstStudent = new List<Student>();
foreach(InternalStudent s in lstInterStudent)
            {
                lstStudent.Add(new Student
                    {
                        Name = s.IName,
                        Age = s.IAge
                    });
            }

There is no problem as such in this way of conversion from one generic list to another generic list. However there is better way to do this conversion.


List<Student> lstStudent = lstInterStudent.ConvertAll(x => new Student
                {
                    Age = x.IAge,
                    Name = x.IName
                });

We are using ConvertAll to convert one generic list to another generic list. I hope you find this post useful. Thanks for reading.

Advertisements

4 thoughts on “Convert List to List using ConvertAll in C#

  1. Sir,
    I don’t know about the ConvertAll and thanks for the information.

    But, there’s one more problem I face. Now, if I want to Convert List to List with a long list of members(for ex, around 100). We can do this also using your ConvertAll but It’s obvious that mapping 100 members is not a efficient code.

    So, what do you suggest for this.

    Thanks in advance,

    Your follower,
    @shivamchopra

  2. Sir,
    I don’t know about the ConvertAll and thanks for the information.

    But, there’s one more problem I face. Now, if I want to Convert List to List with a long list of members(for ex, around 100). We can do this also using your ConvertAll but It’s obvious that mapping 100 members is not a efficient code.

    So, what do you suggest for this.

    Thanks in advance,

    Your follower,
    @shivamchopra

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s