English 中文(简体)
表达树可能不会包含一种模式匹配的操作者。
原标题:An expression tree may not contain an is pattern-matching operator

我试图利用<代码>Automapper绘制这些特性的地图。

public class SourceClass
{
    public object emailNotification { get; set; }
}

public class DestinationClass
{
    public int emailNotificationId { get; set; }
    public string emailNotificationLookupName { get; set; }
}

当I debug it时,email Notification = ValueKind = 反对: “{“id”:3,“lookupName”:“Send Email inform”}"

预期成果

  • emailNotificationId = 3
  • emailNotificationLookupName = "Send Email Notification"

这是我目前的制图代码,但我有这一汇编错误:

public class SourceProfile : AutoMapper.Profile
{
    public SourceProfile()
    {
        CreateMap<SourceClass, DestinationClass>()     
            .ForMember(dest => dest.emailNotificationId, opt => opt.MapFrom(src =>
                src.emailNotification != null && src.emailNotification is JObject jObject ? jObject["id"].Value<int>() : 0))
            .ForMember(dest => dest.emailNotificationLookupName, opt => opt.MapFrom(src =>
                src.emailNotification != null && src.emailNotification is JObject jObject ? jObject["lookupName"].Value<string>() : null))
        ;
                        
    }
}

在尝试了永道的两种解决办法之后,我可以毫不错误地汇编,但我仍然无法绘制出价值图。

“在座的影像描述”/</a

“entergraph

New screenshot enter image description here

问题回答

您可以使用<条码>作为<条码/代码>的操作员,以取代<条码>is,以解决这个问题:

CreateMap<SourceClass, DestinationClass>()     
    .ForMember(dest => dest.emailNotificationId, opt => opt.MapFrom(src =>
        src.emailNotification != null ? JObject.FromObject(src.emailNotification)["id"].Value<int>() : 0))
    .ForMember(dest => dest.emailNotificationLookupName, opt => opt.MapFrom(src =>
        src.emailNotification != null ? JObject.FromObject(src.emailNotification)["lookupName"].Value<string>() : null))

由于认证<代码> 电子邮件通知的逻辑不是null, 并转至 JObjectplica in multi place, You may consider migration in 在Map行动之后:

CreateMap<SourceClass, DestinationClass>()     
    .AfterMap((src, dest, context) =>
    {
        if (src.emailNotification != null)
        {
            JObject jObject = JObject.FromObject(src.emailNotification);
            dest.emailNotificationId = jObject["id"].Value<int>();
            dest.emailNotificationLookupName = jObject["lookupName"].Value<string>();
        }
    });

I found this article about Expression Trees that you might find interesting. It says that you cannot use the pattern matching is operator inside one of these. This answer from JonSkeet himself might be useful too, but summarazing, it is a feature that is not currently supported in Expression Trees (like the one you are passing as parameter to the MapFrom method).

希望会有助于





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签