自动映射器投影和联合 [英] Automapper projection and union
问题描述
我对联合投影和自动映射投影有问题.
I have a problem with union and automapper projections.
我有两个实体:
public class Entity
{
public DateTime ActionDate { get; set; }
public int SomeProp { get; set; }
}
public class ExtendedEntity
{
public DateTime ActionDate { get; set; }
public int SomeProp { get; set; }
public int SomeOtherProp { get; set; }
}
和投影:
public class EntityProjection
{
public DateTime ActionDate { get; set; }
public int SomeProp { get; set; }
public int SomeOtherProp { get; set; }
public string Source { get; set; }
}
我将实体映射到一个投影,因为实体没有SomeOtherProp,所以我将其设置为0:
i map entities to one projection, Entity does not have SomeOtherProp so i set 0 to it:
public class EntityProfile : Profile
{
protected override void Configure()
{
CreateMap<ExtendedEntity, EntityProjection>()
.ForMember(dest => dest.Source, opt => opt.MapFrom(src => "ext entity"));
CreateMap<Entity, EntityProjection>()
.ForMember(dest => dest.SomeOtherProp, opt => opt.MapFrom(src => 0))
.ForMember(dest => dest.Source, opt => opt.MapFrom(src => "entity"));
}
}
当我尝试使用下一个代码时,出现错误:
when i try to use next code i get error:
var entities = context.Set<Entity>()
.Project().To<EntityProjection>();
var extEntities = context.Set<ExtendedEntity>()
.Project().To<EntityProjection>();
var result = entities.Union(extEntities).OrderBy(p => p.ActionDate).ToList();
错误文本:类型'UserQuery + EntityProjection'出现在单个LINQ to Entities查询中的两个结构不兼容的初始化中.类型可以是...
Error text: The type 'UserQuery+EntityProjection' appears in two structurally incompatible initializations within a single LINQ to Entities query. A type can be...
这意味着投影中的属性必须以相同的顺序初始化,我如何通过自动映射器设置投影属性的初始化顺序?
That means that properties in projection must be initialized in same order, how i can set projection properties initialization order by automapper?
推荐答案
答案很晚,简短的版本似乎是你不能".
Very late answer, and the short version seems to be "You can't".
我有完全相同的问题(我可以强制Automapper按一定顺序初始化属性吗?)并最终在LINQ select语句中映射所有内容.
I had exactly the same question (Can I force Automapper to initialise properties in a certain order?) and ended up mapping everything within a LINQ select statement.
为简便起见,我在DTO(缩减代码)中将其设置为静态方法:
For ease, I made it a static method within my DTO (cut-down code):
public static IQueryable<MyDto> QueryableFromTaskType1(
IQueryable<TaskType1> query)
{
return query.Select(src => new MyDto()
{
TaskId = src.Id,
AssetTypeName = src.Asset.AssetType.Name,
AssetId = src.Asset.Id,
AssetCode = src.Asset.Code,
AssetName = src.Asset.Name,
});
}
public static IQueryable<MyDto> QueryableFromTaskType2(
IQueryable<TaskType2> query)
{
return query.Select(src => new MyDto()
{
TaskId = src.Id,
AssetTypeName = src.AssetTypeName,
AssetId = src.AssetId,
AssetCode = src.AssetCode,
AssetName = src.AssetName,
});
}
然后,您可以将对象作为IQueryable获取,只需将它们通过适当的静态方法(将select附加到DTO或其他已知的项目)中进行传递,然后对所得的IQueryable进行联合或合并.
then you can get your objects, as an IQueryable, simply pass them through the appropriate static method (which appends a select into the DTO - or projects as it's otherwise known) and then Union or Concat the resulting IQueryables.
唯一的缺点是Automapper通常会处理递归自动映射,尽管我可以肯定它不会很好地映射到SQL,所以您可能不会损失太多.
The only downside is that Automapper will normally deal with recursive automapping, although I'm pretty certain that wouldn't map to SQL well anyway, so you probably don't lose much.
这篇关于自动映射器投影和联合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!