Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JonLopezGarcia/9218cbd0a85523d781d3f901d0610fcf to your computer and use it in GitHub Desktop.
Save JonLopezGarcia/9218cbd0a85523d781d3f901d0610fcf to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Linq;
using Shouldly;
using Simple;
using Xunit;
namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
{
public class QueryableSelectionWithUseAsDataSource
{
[Fact]
public void When_Select_List_Over_Queryable_As_Data_Source()
{
// Arrange
var mapper = CreateMapper();
var models = new List<A>()
{
new A {
Aas = new List<A.AA>
{
new A.AA { Str = "Hey"},
new A.AA { Str = "Hey"},
new A.AA { Str = "Hey"},
new A.AA { Str = "Hey"}
}
},
new A {
Aas = new List<A.AA>
{
new A.AA { Str = "Hoi"},
new A.AA { Str = "Hoi"},
new A.AA { Str = "Hoi"},
new A.AA { Str = "Hoi"}
}
},
new A {
Aas = new List<A.AA>
{
new A.AA { Str = "Hui"},
new A.AA { Str = "Hui"},
new A.AA { Str = "Hui"},
new A.AA { Str = "Hui"}
}
},
new A {
Aas = new List<A.AA>
{
new A.AA { Str = "Hai"},
new A.AA { Str = "Hai"},
new A.AA { Str = "Hai"},
new A.AA { Str = "Hai"}
}
}
};
var queryable = models.AsQueryable();
// Act
var selectedQueryable = queryable
.UseAsDataSource(mapper)
.For<B>()
.Select(dto => dto.Bas);
var result = selectedQueryable.ToList();
// Assert
result.ShouldNotBeNull();
}
private static IMapper CreateMapper()
{
var mapperConfig = new MapperConfiguration(cfg =>
{
cfg.CreateMap<A, B>().ForMember(b => b.Bas, expression => expression.MapFrom(a => a.Aas));
cfg.CreateMap<B, A>().ForMember(a => a.Aas, expression => expression.MapFrom(b => b.Bas));
cfg.CreateMap<A.AA, B.BA>();
cfg.CreateMap<B.BA, A.AA>();
});
var mapper = mapperConfig.CreateMapper();
return mapper;
}
}
}
namespace Simple
{
public class A
{
public IList<AA> Aas { get; set; }
public class AA
{
public string Str { get; set; }
}
}
public class B
{
public IList<BA> Bas { get; set; }
public class BA
{
public string Str { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment