Skip to content

Commit ccc4223

Browse files
Merge pull request #53 from AutoMapper/FixHashConditionals
Ignore converting conditional expressions
2 parents 51081c8 + e75f7e1 commit ccc4223

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/AutoMapper.Collection.Tests/MapCollectionWithEqualityTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,42 @@ public void Should_Be_Fast_With_Large_Lists_SubObject_WrongCollectionType_Should
163163
a.ShouldThrow<ArgumentException>().Where(x => x.Message.Contains(typeof(ThingSubDto).FullName) && x.Message.Contains(typeof(ThingDto).FullName));
164164
}
165165

166+
public void Should_Work_With_Conditionals()
167+
{
168+
Mapper.Initialize(cfg =>
169+
{
170+
cfg.AddCollectionMappers();
171+
cfg.CreateMap<ClientDto, Client>()
172+
.EqualityComparison((src, dest) => dest.DtoId == 0 ? src.Code == dest.Code : src.Id == dest.DtoId);
173+
});
174+
175+
var dto = new ClientDto
176+
{
177+
Code = "abc",
178+
Id = 1
179+
};
180+
var entity = new Client {Code = dto.Code, Id = 42};
181+
var entityCollection = new List<Client> {entity};
182+
183+
Mapper.Map(new[] {dto}, entityCollection);
184+
185+
entity.ShouldBeEquivalentTo(entityCollection[0]);
186+
}
187+
188+
public class Client
189+
{
190+
public long Id { get; set; }
191+
public string Code { get; set; }
192+
public long DtoId { get; set; }
193+
}
194+
195+
public class ClientDto
196+
{
197+
public long Id { get; set; }
198+
public string Code { get; set; }
199+
}
200+
201+
166202
public void Should_Work_With_Null_Destination()
167203
{
168204
var dtos = new List<ThingDto>

src/AutoMapper.Collection/EquivalencyExpression/HashableExpressionsVisitor.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ internal static Tuple<List<Expression>, List<Expression>> Expand(ParameterExpres
2727
return Tuple.Create(visitor._sourceMembers, visitor._destinationMembers);
2828
}
2929

30+
protected override Expression VisitConditional(ConditionalExpression node)
31+
{
32+
return node;
33+
}
34+
3035
protected override Expression VisitBinary(BinaryExpression node)
3136
{
3237
switch (node.NodeType)

0 commit comments

Comments
 (0)