-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Flattened properties via IncludeMembers don't get mapped correctly in expressions #151
Comments
What you've posted runs for me without an exception. This [Fact]
public void Issuew()
{
var config = new MapperConfiguration(c =>
{
c.CreateMap<Category, ProductDTO>()
.ForMember(p => p.CategoryName, c => c.MapFrom(c => c.Name));
c.CreateMap<Product, ProductDTO>()
.IncludeMembers(p => p.Category);
});
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
Expression<Func<ProductDTO, bool>> expr = x => x.CategoryName == "MyCategory";
//var mappedExpression = mapper.MapExpression<Expression<Func<Product, bool>>>(expr);
IQueryable<Product> products = new List<Product>() { new Product { Category = new Category { Name = "MyCategory" } } }.AsQueryable();
var dto = products.UseAsDataSource(mapper.ConfigurationProvider).For<ProductDTO>().FirstOrDefault(p => p.CategoryName == "MyCategory");
}
public class Category
{
public string? Name { get; set; }
}
public class Product
{
public Category? Category { get; set; }
}
public class ProductDTO
{
public string? CategoryName { get; set; }
} You'll want to post something anyone can copy, paste, run and see the exception. |
I'll check tomorrow better, does it work for Ordering too for you? Like |
It's going to be up to you to reproduce exceptions :). Plenty of tests here including |
I tried again, with the following code: public class TestCategory {
public string? Name { get; set; }
}
public class TestProduct {
public TestCategory? Category { get; set; }
}
public class TestProductDTO {
public string? Name { get; set; }
}
void Test(){
var config = new MapperConfiguration(c =>
{
c.CreateMap<TestCategory, TestProductDTO>();
c.CreateMap<TestProduct, TestProductDTO>()
.IncludeMembers(p => p.Category);
});
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
var products = new List<TestProduct>() {
new TestProduct {
Category = new TestCategory { Name = "MyCategory" }
}
}.AsQueryable();
Expression<Func<TestProductDTO, bool>> expr = x => x.Name == "MyCategory";
var mappedExpression = mapper.MapExpression<Expression<Func<TestProduct, bool>>>(expr);
var aaa = products.UseAsDataSource(mapper.ConfigurationProvider).For<TestProductDTO>()
.FirstOrDefault(x => x.Name == "MyCategory");
} And I get weird results...
The stack trace is a little bit different:
|
The recommended approach is the following: [Fact]
public void Issue()
{
var config = new MapperConfiguration(c =>
{
c.CreateMap<TestCategory, TestProductDTO>();
c.CreateMap<TestProduct, TestProductDTO>()
.IncludeMembers(p => p.Category);
});
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
var products = new List<TestProduct>() {
new TestProduct {
Category = new TestCategory { Name = "MyCategory" }
}
}.AsQueryable();
Expression<Func<TestProductDTO, bool>> expr = x => x.Name == "MyCategory";
var mappedExpression = mapper.MapExpression<Expression<Func<TestProduct, bool>>>(expr);
products = products.Where(mappedExpression);
var result = mapper.ProjectTo<TestProductDTO>(products).FirstOrDefault();
//var aaa = products.UseAsDataSource(mapper.ConfigurationProvider).For<TestProductDTO>()
//.FirstOrDefault(x => x.Name == "MyCategory");
} It is less "black boxed" for one thing. Also filtering before projection is recommended - see this issue from a month ago. The ReadMe has a couple of examples of extension methods - using Note that the expression mapping code in |
This time I think this issue is related to this library.
I have a model and dtos like this:
If I create the maps "manually", everything works:
I can then query it regularly:
But if I try to include the Category entity:
Then I get an exception in the query:
Here's the full stack trace:
The text was updated successfully, but these errors were encountered: