Mapping items from one class to another if Items are equivalent

前端 未结 4 1331
鱼传尺愫
鱼传尺愫 2021-01-16 02:53

Say I have one class that looks like this:

public class Person
{
     public string Name {get; set;}
     public int Number {get; set;}
}
         


        
4条回答
  •  执笔经年
    2021-01-16 03:11

    If you don't work with big generic list, you can do it using LinQ.

    var persons = new List();
    // populate data [...]
    var dogs = persons.Select(p=>new Dog{Name=p.Name,Number=p.Number}).ToList();
    

    It's easy to remember, and you can filter data previously.

提交回复
热议问题