c# - convert a list of objects from one type to another using lambda expression


Translate

I have a foreach loop reading a list of objects of one type and producing a list of objects of a different type. I was told that a lambda expression can achieve the same result.

var origList = List<OrigType>(); // assume populated
var targetList = List<TargetType>(); 

foreach(OrigType a in origList) {
    targetList.Add(new TargetType() {SomeValue = a.SomeValue});
}

Any help would be appreciated- i'm new to lambda and linq thanks, s


All Answers
  • Translate

    Try the following

    var targetList = origList
      .Select(x => new TargetType() { SomeValue = x.SomeValue })
      .ToList();
    

    This is using a combination of Lambdas and LINQ to achieve the solution. The Select function is a projection style method which will apply the passed in delegate (or lambda in this case) to every value in the original collection. The result will be returned in a new IEnumerable<TargetType>. The .ToList call is an extension method which will convert this IEnumerable<TargetType> into a List<TargetType>.


  • Translate

    If you know you want to convert from List<T1> to List<T2> then List<T>.ConvertAll will be slightly more efficient than Select/ToList because it knows the exact size to start with:

    target = orig.ConvertAll(x => new TargetType { SomeValue = x.SomeValue });
    

    In the more general case when you only know about the source as an IEnumerable<T>, using Select/ToList is the way to go. You could also argue that in a world with LINQ, it's more idiomatic to start with... but it's worth at least being aware of the ConvertAll option.


  • Translate
    var target = origList.ConvertAll(x => (TargetType)x);
    

  • Translate
    List<target> targetList = new List<target>(originalList.Cast<target>());
    

  • Translate

    I believe something like this should work:

    origList.Select(a => new TargetType() { SomeValue = a.SomeValue});
    

  • Translate

    Here's a simple example..

    List<char> c = new List<char>() { 'A', 'B', 'C' };
    
    List<string> s = c.Select(x => x.ToString()).ToList();
    

  • Translate
    var list1 = new List<Type1>();
    var list2 = new List<Type2>();
    
    list1.ForEach(item => list2.Add(new Type2() { Prop1 = value1 }));
    

  • Translate

    Assume that you have multiple properties you want to convert.

    public class OrigType{
        public string Prop1A {get;set;}
        public string Prop1B {get;set;}
    }
    
    public class TargetType{
        public string Prop2A {get;set;}
        public string Prop2B {get;set;}
    }
    
    var list1 = new List<OrigType>();
    var list2 = new List<TargetType>();
    
    list1.ConvertAll(x => new OrigType { Prop2A = x.Prop1A, Prop2B = x.Prop1B })
    

  • Translate

    If you need to use a function to cast:

    var list1 = new List<Type1>();
    var list2 = new List<Type2>();
    
    list2 = list1.ConvertAll(x => myConvertFuntion(x));
    

    Where my custom function is:

    private Type2 myConvertFunction(Type1 obj){
       //do something to cast Type1 into Type2
       return new Type2();
    }
    

  • Translate

    Or with a constructor & link with Select:

    public class TargetType {
      public string Prop1 {get;set;}
      public string Prop1 {get;set;}
    
      // Constructor
      public TargetType(OrigType origType) {
        Prop1 = origType.Prop1;
        Prop2 = origType.Prop2;
      }
    }
    
    var origList = new List<OrigType>();
    var targetList = origList.Select(s=> new TargetType(s)).ToList();  
    

    The Link line is more soft! ;-)


  • Translate

    for similar type class.

    List<targetlist> targetlst= JsonConvert.DeserializeObject<List<targetlist>>(JsonConvert.SerializeObject(<List<baselist>));