|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Linq.Expressions; |
| 5 | +using System.Text; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace AutoMapper.Extensions.ExpressionMapping.UnitTests |
| 9 | +{ |
| 10 | + public class EnumerableDotContainsWorksOnLocalVariables |
| 11 | + { |
| 12 | + [Fact] |
| 13 | + public void Issue87() |
| 14 | + { |
| 15 | + var config = new MapperConfiguration(cfg => |
| 16 | + { |
| 17 | + cfg.AddExpressionMapping(); |
| 18 | + |
| 19 | + cfg.CreateMap<Source, SourceDto>() |
| 20 | + .ForMember(o => o.Items, config => config.MapFrom(p => p.Items.Select(s => s.Name))); |
| 21 | + }); |
| 22 | + |
| 23 | + var mapper = config.CreateMapper(); |
| 24 | + |
| 25 | + var items = new string[] { "item1", "item2" }; |
| 26 | + Expression<Func<SourceDto, bool>> expression1 = o => items.Contains("item1"); |
| 27 | + Expression<Func<SourceDto, bool>> expression2 = o => items.Contains(""); |
| 28 | + Expression<Func<SourceDto, bool>> expression3 = o => o.Items.Contains("item1"); |
| 29 | + Expression<Func<SourceDto, bool>> expression4 = o => o.Items.Contains("B"); |
| 30 | + |
| 31 | + var mapped1 = mapper.MapExpression<Expression<Func<Source, bool>>>(expression1); |
| 32 | + var mapped2 = mapper.MapExpression<Expression<Func<Source, bool>>>(expression2); |
| 33 | + var mapped3 = mapper.MapExpression<Expression<Func<Source, bool>>>(expression3); |
| 34 | + var mapped4 = mapper.MapExpression<Expression<Func<Source, bool>>>(expression4); |
| 35 | + |
| 36 | + Assert.Equal(1, new Source[] { new Source { } }.AsQueryable().Where(mapped1).Count()); |
| 37 | + Assert.Equal(0, new Source[] { new Source { } }.AsQueryable().Where(mapped2).Count()); |
| 38 | + Assert.Equal(1, new Source[] { new Source { Items = new List<SubSource> { new SubSource { Name = "item1" } } } }.AsQueryable().Where(mapped3).Count()); |
| 39 | + Assert.Equal(0, new Source[] { new Source { Items = new List<SubSource> { new SubSource { Name = "" } } } }.AsQueryable().Where(mapped4).Count()); |
| 40 | + } |
| 41 | + |
| 42 | + public class Source { public ICollection<SubSource> Items { get; set; } } |
| 43 | + |
| 44 | + public class SubSource { public int ID { get; set; } public string Name { get; set; } } |
| 45 | + |
| 46 | + public class SourceDto { public string[] Items { get; set; } } |
| 47 | + } |
| 48 | +} |
0 commit comments