自动映射器投影和联合

编程入门 行业动态 更新时间:2024-10-26 19:26:59
本文介绍了自动映射器投影和联合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对联合投影和自动映射投影有问题.

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.

更多推荐

自动映射器投影和联合

本文发布于:2023-11-04 12:08:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557899.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:映射器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!