[英]Cast System.Data.Linq.Table<T> to custom class
I do a request on my sql table using Linq to SQL
. 我使用
Linq to SQL
在我的sql表上做了一个请求。 I get a instance of System.Data.Linq.Table<Table>
我得到一个
System.Data.Linq.Table<Table>
的实例
I have also a class with some properties which corresponds to the properties of my table (The properties have the same name as the properties of my instance of System.Data.Linq.Table<Table>
) 我还有一个类,其中包含一些与我的表的属性相对应的属性(这些属性与我的
System.Data.Linq.Table<Table>
实例的属性相同)
How to cast the System.Data.Linq.Table<Table>
to a instance of my custom class ? 如何将
System.Data.Linq.Table<Table>
转换为我的自定义类的实例?
Without do Select(x => new CustomClass() { Id = x.Id, Name = x.Name ....})
and without (if possible) using Reflexion
(it's so slow) 没有做
Select(x => new CustomClass() { Id = x.Id, Name = x.Name ....})
而没有(如果可能的话)使用Reflexion
(它太慢了)
You can use AutoMapper to map your objects You can check the getting started page here https://github.com/AutoMapper/AutoMapper/wiki/Getting-started 您可以使用AutoMapper映射您的对象您可以在此处查看入门页面https://github.com/AutoMapper/AutoMapper/wiki/Getting-started
Basically you'll setup a mapping between your Table
and CustomClass
classes 基本上,您将在
Table
和CustomClass
类之间设置映射
Mapper.Initialize(cfg => cfg.CreateMap<Table, CustomClass>());
Then you can map a Table
instance to a CustomClass
one 然后,您可以将
Table
实例映射到CustomClass
实例
CustomClass myCustomClass= Mapper.Map<CustomClass>(table);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.