Click here to Skip to main content
15,914,222 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I have to classes –
C#
public class Agent
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string Thana { get; set; }
        public int District { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public string NID { get; set; }
        public bool IsDeleted { get; set; }
        public string AddedBy { get; set; }
        public System.DateTime DateAdded { get; set; }
        public string UpdatedBy { get; set; }
        public Nullable<System.DateTime> DateUpdated { get; set; }
    }

public class AgentModel
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string Thana { get; set; }
        public int District { get; set; }
        public string Phone { get; set; }
        public string Email { get; set; }
        public string NID { get; set; }
        public bool IsDeleted { get; set; }
        public string AddedBy { get; set; }
        public DateTime DateAdded { get; set; }
        public string UpdatedBy { get; set; }
        public DateTime DateUpdated { get; set; }
    }

How to convert one type of generic list to another type of generic list in C#?
For example - I want to convert list to list2.
C#
List<agent> list =    new List<agent>();
List<agentmodel> list2 = new List<agentmodel>();


And, how I can convert one object to another type of object?
For instance, I want to convert also Agent to agent2.
C#
Agent agent = new Agent();
AgentModel agent2 = new AgentModel();


The main problem is my project already developed based on SQL server, and factory pattern is being used in my project. That's why, i just want to change only DataAccessManager class. Moreover, we named all model classes after all data table name (i.e for data table named Agent, the class name is AgentModel; for the data table product, the class name is ProductModel). However, my management wanted to use Entity Framework. As a result, i have followed DB first approach as i already have data tables. However, problem is that EF generate classes with the same name of data tables. Thus, the conversion is required. So, is there any way to name EF classes as like existing classes? or what should be the best solution?


Thanks in advance.
Posted
Updated 23-Mar-15 7:45am
v3
Comments
Sergey Alexandrovich Kryukov 23-Mar-15 9:17am    
Even the beginning of the code, definitions of Agent and AgentModel is super-puper abuse, defeat the purpose of OOP and common sense. You should not repeat any lines in code, this means defeat the purpose of programming. I'm just curious: do you know inheritance?
—SA
Attiq-ul Islam 23-Mar-15 13:43pm    
Yes, I know inheritance, i also know this is not any way of OOP concept. However, the main problem is my project already developed based on SQL server, and factory pattern is being used in my project. That's why, i just want to change only DataAccessManager class. Moreover, we named all model classes after all data table name (i.e for data table named Agent, the class name is AgentModel; for the data table product, the class name is ProductModel). However, my management wanted to use Entity Framework. As a result, i have followed DB first approach as i already have data tables. However, problem is that EF generate classes with the same name of data tables. Thus, the conversion is required. So, is there any way to name EF classes as like existing classes? or what should be the best solution?
Sergey Alexandrovich Kryukov 23-Mar-15 14:12pm    
You see, the explanation of the abuse does not turn this abuse into no abuse. Anyway, I answered your question, haven't I?
—SA
Attiq-ul Islam 23-Mar-15 14:49pm    
yes, you are right. However, should i use reflection to solve the problem or there is a better way to introduce EF in my project?
Sergey Alexandrovich Kryukov 23-Mar-15 15:15pm    
I would not do it. Reflection is slow, first of all. You can is inheritance. Yes, you can.
—SA

There are nearly identical structures. There is no way to directly convert from one type to the other without making an entirely new set of objects.

One thing stopping you from doing the conversion is that AgentModel has a non-nullable DateTime in it where the class you're trying to convert from has a nullable DateTime. You cannot convert from the nullable type to the non-nullable. I think the reason why is perfectly clear.


Really the difference between the two classes is just that nullable DateUpdated field. You really don't even need two classes since they are essentially the same. Just drop the AgentModel class and use the Agent class, with the nullable DateTime for DateUpdated.


You don't specify what these classes are used for so it's impossible to be more specific with a better solution.
 
Share this answer
 
Comments
Attiq-ul Islam 23-Mar-15 15:36pm    
first of all thanks a lot for your cooperation. I explained my situation again. Please see the question.
Dave Kreskowiak 23-Mar-15 17:52pm    
I haven't done DB First with EF so I can't comment on it. But, with Code First, yes, you can call your classes whatever you want. You just have to manually map the classes to the tables they are stored in the database. The downside to what you're doing is that you have to map every single table you use. Depending on the size of the database, that can be a lot of work.
Attiq-ul Islam 24-Mar-15 1:33am    
Hmm. Thanks a lot once again.
You cannot do it because the element types are unrelated (isn't it obvious?).
You can always create an instance of the target type and copy data element by element. But the whole idea is wrong; you just abuse programming by having two unrelated classes with similar members.

It they were related, the answer would depend on how and the .NET version. Please see:
https://msdn.microsoft.com/en-us/library/dd799517%28v=vs.110%29.aspx[^],
http://en.wikipedia.org/wiki/Covariance_and_contravariance_%28computer_science%29[^].

—SA
 
Share this answer
 
Comments
Attiq-ul Islam 23-Mar-15 14:43pm    
Yes, you are right. Moreover,those are very good tutorials.
However, should i use reflection?

Thanks a lot for your kind cooperation.
Sergey Alexandrovich Kryukov 23-Mar-15 15:15pm    
Very good. Reflection? It depends on what purpose. In this case, you don't need it.
—SA
Attiq-ul Islam 23-Mar-15 15:33pm    
But, what is the better option? could you please give me an example?
Sergey Alexandrovich Kryukov 23-Mar-15 15:45pm    
Option for what? I already explain what you could do having your type declaration. Write "copy" or "clone" method creating instance of one type out of the instance of another. Clone elements one by one and add to a new list.
—SA
Attiq-ul Islam 23-Mar-15 16:26pm    
Thanks a lot.
XML
List<Agent> list = new List<Agent> { new Agent { ID = 123, Name = "John" } };

// copy across all params
List<AgentModel> list2 = list.Select(a => new AgentModel { ID = a.ID, Name = a.Name }).ToList();


There are libraries that make this kind of thing easier like autopmapper.

http://automapper.org/[^]

You might also want to consider using inheritance given there is a lot of cross-over in your classes. Or give the AgentModel class an Agent property that contains the Agent class, having only properties the AgentModel class that apply to the model but not the Agent.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 23-Mar-15 9:26am    
Yes, this is the solution. But is the abuse obvious? Please see my comment to the question and Solution 2.
(No, I did not down-vote the answer.)
—SA
F-ES Sitecore 23-Mar-15 9:39am    
Yes the abuse is obvious :) But he asked a question so I gave him an answer *shrug* as well as a nod toward a better direction he can go in. Rome wasn't built in a day :)
Sergey Alexandrovich Kryukov 23-Mar-15 10:05am    
I have to agree. But I would suggest the inquirer to do smaller steps and not to abuse anyway. OOP should be mastered in its basics well before the point he is approaching here.
—SA
Attiq-ul Islam 23-Mar-15 14:46pm    
the solution should use generic list. However, thanks a lot for your kind cooperation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900