简体   繁体   中英

Copy from one list of objects to another with the same structure

How to copy all from one list of object to another one. Both objects are with the same structure but with different name.

Here is the code:

 class Program
{
    static void Main(string[] args)
    {
        List<Test> lstTest = new List<Test>();
        List<Test2> lstTest2 = new List<Test2>();

        lstTest.Add(new Test { Name = "j", Score = 2 });
        lstTest.Add(new Test { Name = "p", Score = 3 });

        lstTest2 = lstTest.ConvertAll(x => (Test)x);

    }
}

class Test
{
    private string name;
    private int score;

    public string Name
    {
        get { return name;  }
        set { this.name = value; }
    }

    public int Score
    {
        get { return score; }
        set { this.score = value; }
    }
}

class Test2
{
    private string name;
    private int score;

    public string Name
    {
        get { return name; }
        set { this.name = value; }
    }

    public int Score
    {
        get { return score; }
        set { this.score = value; }
    }
}

Error that I get is

Cannot implicitly convert type System.Collections.Generic.List<Test> to System.Collections.Generic.List<cTest2>

If you do not want to use automapper or other mapping tools, you can do this like this using select and new instance then return a list:

lstTest2 = lstTest.Select(e => new Test2()
{
    Score = e.Score,
    Name = e.Name
}).ToList();

In case of Automapper you can do something like:

var config = new MapperConfiguration(cfg => {

    cfg.CreateMap<Test, Test2>();
});
IMapper iMapper = config.CreateMapper();
lstTest2 = iMapper.Map<List<Test>, List<Test2>>(lstTest);

in config you define the type conversion. The map it from one to the other type.

You can of course extend your implementation to make it generic.

Documentation reference:

You are trying to convert implicitly Test to Test2 objects. A simple way to correct your code is to construct Test2 objects:

lstTest2 = lstTest.ConvertAll(x => new Test2 { Name = x.Name, Score = x.Score });

Even if the underlying structure is identical, you cannot cast from Test to Test2 . If you want to cast explicitly, you have to define a cast operator:

class Test2 {
    // all code of class Test2

    public static explicit operator Test2(Test v)
    {
        return new Test2 { Name = v.Name, Score = v.Score };
    }
}

Then you can cast in ConvertAll :

lstTest2 = lstTest.ConvertAll(x => (Test2)x);

Instead of having two completely different objects with different names, study how to do object inheritance .

class Program
{
    static void Main(string[] args)
    {
        List<TestBase> lstTest = new List<TestBase>();

        lstTest.Add(new Test { Name = "j", Score = 2 });
        lstTest.Add(new Test2 { Name = "p", Score = 3 });
    }
}

class TestBase
{
    private string name;
    private int score;

    public string Name
    {
        get { return name;  }
        set { this.name = value; }
    }

    public int Score
    {
        get { return score; }
        set { this.score = value; }
    }
}

class Test : TestBase { }

class Test2 : TestBase { }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM