Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map properties of two different objects?

Tags:

c#

object

oop

I want to know how to map fields of two different objects and assign the values to it.

Eample:

public class employee
{
    public int ID { get; set; }
    public string Name { get; set; }
}

public class manager
{
    public int MgrId { get; set; }
    public string MgrName { get; set; }
}

Now I have a List object. I want to assign the values to "manager" class. Any automatic way to do that. I can do it explicitly and assigning values to it. But my object is very huge thats the problem. I dont want to use any third party tools too.

Note: It can't have any prefix for manager. It can be anything. (Ex: mgrId can be like mgrCode)

like image 513
Murugavel Avatar asked Jul 16 '13 11:07

Murugavel


1 Answers

I know this post is kind old now but for other people who are looking for answer is this what you can go for.. just simply use lambda expression

var managers = employees.Select(x => new Manager()
{
    MgrCode = x.ID,
    MgrName = x.Name
}).ToList();
like image 84
B8ightY Avatar answered Sep 21 '22 01:09

B8ightY