自动映射器映射功能 [英] Automapper mapping function

查看:93
本文介绍了自动映射器映射功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我试图在Automapper中实现相对简单的操作,但是我一直失败. 我正在尝试使用函数表达式在Dto上映射属性

Hello everyone I am trying to achieve something relatively simple in Automapper but I keep on failing. I am trying to map a property on my Dto with a function expression

   .ForMember(dest => dest.HasPaid, opt => opt.MapFrom(c => MapHasPaid(c)))

    private bool MapHasPaid(AppUser src)
    {
        var lastPayment = src.RentPayments.OrderByDescending(p => p.DateTo).FirstOrDefault();
        if (lastPayment == null)
        {
            return false;
        }

        return lastPayment.DateFrom.Date <= DateTime.Now.Date &&
               DateTime.Now.Date <= lastPayment.DateTo.Date;
    }

这将返回

LINQ to Entities无法识别方法"Boolean MapHasPaid(xxxx.Models.AppUser)"方法,并且该方法无法转换为商店表达式.

LINQ to Entities does not recognize the method 'Boolean MapHasPaid(xxxx.Models.AppUser)' method, and this method cannot be translated into a store expression.

我尝试了IValueResolver

I tried with a IValueResolver

public class CustomResolver : IValueResolver<AppUser, HouseMateEntity, bool>
{
    public bool Resolve(AppUser source, HouseMateEntity destination, bool member, ResolutionContext context)
    {
        var lastPayment = source.RentPayments.OrderByDescending(p => p.DateTo).FirstOrDefault();
        if (lastPayment == null)
        {
            return false;
        }

        return lastPayment.DateFrom.Date <= DateTime.Now.Date &&
               DateTime.Now.Date <= lastPayment.DateTo.Date;
    }
}


.ForMember(dest => dest.HasPaid, opt => opt.ResolveUsing<CustomResolver>())

但是请给我以下例外情况

but give me the following exception

无法将其解析为可查询表达式

Can't resolve this to Queryable Expression

如何通过更复杂的查询来映射我的媒体资源?

How can I map my property as a result of more complex query?

谢谢

我也尝试过文档中的简单案例 https://github.com/AutoMapper/AutoMapper/wiki/Custom-value-解析器

I have also tried the simple case on the documentation https://github.com/AutoMapper/AutoMapper/wiki/Custom-value-resolvers

相同的错误异常

无法将其解析为可查询表达式

Can't resolve this to Queryable Expression

有人有ResolveUsing可以工作吗?这是automapper的错误吗?

Has anyone got ResolveUsing to work? is this an automapper bug?

我可以通过以下方法实现此目的的唯一方法,这对我来说真的很难闻

the only way I could get this working is with the following, and it really smells bad to me

.ForMember(dest => dest.HasPaid,
                   opt => opt.MapFrom(c => DbFunctions.TruncateTime(c.RentPayments.OrderByDescending(p => p.DateTo).FirstOrDefault().DateFrom) <= DbFunctions.TruncateTime(DateTime.Now) &&
                                      DbFunctions.TruncateTime(DateTime.Now) <= DbFunctions.TruncateTime(c.RentPayments.OrderByDescending(p => p.DateTo).FirstOrDefault().DateTo)));

推荐答案

我希望您正在尝试将其与AutoMapper的Projection一起使用?问题是您正在尝试实现的Resolve函数正在进行在LINQ中不可翻译为实体的调用.基本上,LINQ在您要进行的一些C#调用与要在数据库上运行的某些相应SQL之间没有映射.

I expect you are attempting to use this with AutoMapper's Projection? The issue is that the Resolve functions you are attempting to implement are making calls that are not translatable in LINQ to Entities. Basically LINQ does not have a mapping between one of the C# calls you're making to some corresponding SQL to run on the database.

最后一段代码起作用的原因是因为您正在使用DbFunctions类,该类实现了所有必要的逻辑来处理LINQ to Entities或LINQ to SQL中的日期时间操作.

The reason your last bit of code works is because you are using the DbFunctions class which implements all the necessary logic to handle datetime manipulation in LINQ to Entities or LINQ to SQL.

要么完全按照您的方式使用DbFunctions方法,要么先检索数据(在查询中调用.ToList()或类似的方法),然后映射到您的Target类型.

Either use the DbFunctions methods exactly like you are or first retrieve the data (call .ToList() or similar on the query) and then map to your Target type.

这已记录在 AutoMapper文档中.

如果您使用的是AutoMapper可查询扩展,则不支持ResolveUsing

If you are using the AutoMapper Queryable Extensions then ResolveUsing is NOT supported AutoMapper documentation: Supported mapping options

这篇关于自动映射器映射功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆