【发布时间】:2012-02-23 11:09:56
【问题描述】:
是否有在实体框架中创建自动投影?请看:
public class Person{
public int Id {get; set;}
public string FirstName {get; set;}
public string LastName {get; set;}
public string FatherName {get; set;}
public string City {get; set;}
public string AddressLine {get; set;}
public string Something {get; set;}
}
public class PersonNameModel{
public string FirstName {get; set;}
public string LastName {get; set;}
public string FatherName {get; set;}
}
public class PersonAddressModel{
public string City {get; set;}
public string AddressLine {get; set;}
}
// etc...
我的意思是我可以像这样替换普通投影:
context.Persons.Select(t => new PersonNameModel{ FirstName = t.FirstName /* etc */ });
使用可以使用反射并创建自动投影的扩展方法,例如:
public static class MyExtensions{
public static IQueryable<T> AutoSelect<T, TProject>(this IQueryable<T> q){
// read TProject type in reflection
// create a projection as a IQueryable<T>
}
}
有什么办法吗?我用谷歌搜索了它,但没有找到任何资源。可以指导一下吗?
【问题讨论】:
标签: entity-framework c#-4.0 reflection extension-methods projection