Skip to content

Parameter not remapped when using a logical binary expression #126

Closed
@maboivin

Description

@maboivin

Suppose the following LINQPad program:

void Main()
{
    var mapper = new Mapper(new MapperConfiguration(config =>
    {
        config.CreateMap<Shape, ShapeDto>()
            .ForMember(dto => dto.ShapeType, o => o.MapFrom(s => s is Triangle ? ShapeType.Triangle : s is Circle ? ShapeType.Circle : ShapeType.Unknown))
            .ForMember(dto => dto.DynamicProperties, o => o.Ignore());

        //config.CreateMap<Triangle, ShapeDto>()
        //   .IncludeBase<Shape, ShapeDto>();
        //           
        //config.CreateMap<Circle, ShapeDto>()
        //    .IncludeBase<Shape, ShapeDto>();
    }));

    // Where clause where the parameter is not remapped.
    Expression<Func<ShapeDto, bool>> where = x => x.ShapeType == ShapeType.Circle;

    // Where clause where the parameter is remapped.
    //Expression<Func<ShapeDto, bool>> where = x => x.Name == "A";

    var whereMapped = mapper.MapExpression<Expression<Func<Shape, bool>>>(where).Dump();

    whereMapped.Compile()(new Circle()).Dump();
}

// Define other methods and classes here
public class Shape
{
    public string Name { get; set; }
}

public class Triangle : Shape
{
    public double A { get; set; }
    public double B { get; set; }
    public double C { get; set; }
}

public class Circle : Shape
{
    public double R { get; set; }
}

public enum ShapeType
{
    Unknown,
    Triangle,
    Circle,
}

public class ShapeDto
{
    public string Name { get; set; }
    public IDictionary<string, object> DynamicProperties { get; set; }
    public ShapeType ShapeType { get; set; }
}

When mapping the where expression x => x.ShapeType == ShapeType.Circle, it should map to x => (Convert(IIF((x Is Triangle), Triangle, IIF((x Is Circle), Circle, Unknown))) == 2) but instead, it maps to x => (Convert(IIF((s Is Triangle), Triangle, IIF((s Is Circle), Circle, Unknown))) == 2) where the parameter x is not remapped to parameter s.

It ultimately throws an InvalidOperationException with the message variable 's' of type 'UserQuery+Shape' referenced from scope '', but it is not defined.

Is there a configuration missing or could it be related to a specific case not handled in the XpressionMapperVisitor?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions