更新时间:2022-05-29 23:14:31
答案很晚,简短的版本似乎是你不能".
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.