如何在C#中将一种通用列表转换为另一种类型的通用列表? [英] how to convert one type of generic list to another type of generic list in C#?

查看:79
本文介绍了如何在C#中将一种通用列表转换为另一种类型的通用列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须上课 -

I have to classes –

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; }
    }



如何在C#中将一种类型的通用列表转换为另一种类型的通用列表?

例如 - 我想要将列表转换为list2。


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.

List<agent> list =    new List<agent>();
List<agentmodel> list2 = new List<agentmodel>();





以及如何我可以将一个对象转换为另一种类型的对象吗?

例如,我想将Agent转换为agent2。



And, how I can convert one object to another type of object?
For instance, I want to convert also Agent to agent2.

Agent agent = new Agent();
AgentModel agent2 = new AgentModel();





主要问题是我的项目已经基于SQL服务器开发,工厂模式正在我的项目中使用。这就是为什么,我只想更改DataAccessManager类。此外,我们在所有数据表名称之后命名所有模型类(即,对于名为Agent的数据表,类名为AgentModel;对于数据表产品,类名为ProductModel)。但是,我的管理层想要使用Entity Framework。结果,我已经遵循DB第一种方法,因为我已经拥有数据表。但是,问题是EF生成具有相同名称的数据表的类。因此,需要转换。那么,有没有办法将EF类命名为现有类?或者什么应该是最好的解决方案?





提前致谢。



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.

推荐答案

结构几乎相同。没有制作一组全新的对象就无法直接从一种类型转换为另一种类型。



阻止你进行转换的一件事是AgentModel有一个不可为空的DateTime,其中您尝试转换的类具有可为空的DateTime。您无法从可空类型转换为非可空类型。我认为其原因非常清楚。





两个类之间的差异实际上只是可以为空的DateUpdated字段。你甚至不需要两个类,因为它们基本相同。只需删除AgentModel类并使用Agent类,使用DateUpdated可为空的DateTime。





您没有指定这些类因此使用更好的解决方案是不可能的。
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.


你不能这样做,因为元素类型是不相关的(不是很明显吗?)。

您始终可以创建目标类型的实例并按元素复制数据。但整个想法是错误的;你只是通过让两个不相关的类与类似的成员滥用编程。



它们是相关的,答案取决于如何和.NET版本。请参阅:

https:// msdn .microsoft.com / zh-CN / library / dd799517%28v = vs.110%29.aspx [ ^ ],

http://en.wikipedia.org/wiki/Covariance_and_contravariance_%28computer_science%29 [ ^ ]。



-SA
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


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();





有些图书馆可以像autopmapper一样使这种事情变得更容易。



< a href =http://automapper.org/> http://automapper.org/ [ ^ ]



您可能还想考虑使用继承,因为您的类中有很多交叉。或者为AgentModel类提供一个包含Agent类的Agent属性,该属性只包含适用于模型而不是代理的AgentModel类属性。



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.


这篇关于如何在C#中将一种通用列表转换为另一种类型的通用列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆