Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a list of one object type from a list of another using Linq

If I have classes of Type A and B:

public class A {     public int TotalCount;     public string Title; }  public class B {     public int Count;     public string Title; } 

I have a list of instances A instances, what is the most efficient way to create and populate a List of type B using Linq?

like image 226
Jeremy Avatar asked Nov 09 '09 00:11

Jeremy


1 Answers

List<B> listB = listA.Select(a => new B()    {         Count = a.TotalCount,         Title = a.Title    }).ToList(); 

Does the same as eduncan's solution with different syntax. Take your pick..

like image 118
Daniel M Avatar answered Oct 03 '22 09:10

Daniel M