AutoMapper/AutoMapper — pull request #149 by krlm (2011-11-13 10:18:56+00:00)
Title: Fix: Linq autoprojecting

Hi,

Those commits resolves a two problems with the autoprojecting mechanism.
One it's related to using properties with an enum type in destination class and underlying type of that enum in source class. It may looks like:

Mapper.CreateMap()
.ForMember(dest => dest.Color, opt => opt.MapFrom(src => (Colors)src.Color))
;
where dest.Color is an enum : int Colors { Red, Black } and src.Color is an integer.

The second problem it's related to the CoversionVisitor which should replace in an expression only parameters of source type.

Here's the example:

Mapper.CreateMap()
.ForMember(m => m.Sum, opt => opt.MapFrom(src => src.Collection.Sum(a => a % 2 == 0 ? 1 : 0))
;

var source = new [] {
new Source(),
new Source(),
new Source()
};

var destination = source.AsQueryable().Project()
.To()
.Where(a => a.Sum > 4);
destination.Dump();
}

public class Source
{
public ICollection Collection
{
get
{
return Enumerable.Range(1, 100).ToList();
}
}
}

public class Destination
{
public int Sum { get; set; }
}

Old version of ConversionVisitor would replace the parameter of the sum expression which generates the exception. I've tested it with EF 4.2.
Sorry for a lack of test cases... but I've got no time to prepare them.