如何将Entity类解析为具有Extension的自定义Entity类
本文关键字:Entity Extension 自定义 | 更新日期: 2023-09-27 18:20:16
我想编写一个自定义类扩展。我有EDMX类,但我想解析为CUSTOM类。例如,Customer
类有3个属性(id、name、surface),而我的自定义Customer
类则有3个特性(id、name。我想使用扩展名将List<Customer>
解析为List<MyCustomer>
,但如何做到这一点?
public partial class MyVisitorAdvertisement
{
public int ID { get; set;}
public int VID { get; set;}
public int TID { get; set;}
public string Detail{ get; set;}
public DateTime Date { get; set;}
public DateTime LastDate { get; set;}
public MyVisitorAdvertisement()
{
//constructor
}
public List<MyVisitorAdvertisement> ListByID( int ID)
{
List<MyVisitorAdvertisement> visitors = new List<MyVisitorAdvertisement>();
using (var Ctx = new DomainRepository<VisitorAdvertisement>(new ProposalsEntities()))
{
foreach (var visitor in Ctx.Find<VisitorAdvertisement>(q => q.ID == ID).ToList())
{
MyVisitorAdvertisement visitoradvertisement = new MyVisitorAdvertisement();
visitoradvertisement.ID = visitor.ID;
// i dislike to do it. i think that how to make it a extention?
visitors.Add(visitoradvertisement);
}
return visitors;
}
}
}
我想要的分机:
public static List<TResult> (this IEnumerable<T> mylist) where TResult,T: class
{
return //mylist parse to List<TResult>
}
类似于:
public static List<TResult> ListByID<TResult>(this IEnumerable<T> mylist) where TResult : VisitorAdvertisement where T: class
{
return Ctx.Find<TResult>(q => q.ID == ID).SelectMany(a => new TResult{ Id = a.Id}).ToList();
}
如果VisitorAdvertisement继承自一个包含ID的基本实体,那么您可以基于该实体进行重新限制,而不是向从中派生的任何东西开放。很抱歉,我一直语法错误,我没有在VS.Net 中对此进行编码
看看Automapper:http://automapper.codeplex.com/