-
-
Notifications
You must be signed in to change notification settings - Fork 149
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
Support Select/Projection of an IQueryable #214
Comments
This is something we would like to implement. However, this needs some effort. Rel. #99 |
Idea on how this could work: A partial method for the projection is defined in the mapper: public static partial IQueryable<CarDto> ProjectToDto(this IQueryable<Car> q); To manually configure properties, additional mapping methods are defined in the mapper as needed: [MapProperty("Model.Name", "ModelName")]
public static partial CarDto ToDto(Car car); Mapperly generates the implementation of the projection method based on the configurations on the other defined methods, if available. Complete example: // classes
public class Car
{
public int Id { set; get; }
public Manufacturer Manufacturer { set; get; } = new();
}
public class Manufacturer
{
public string Name { set; get; } = string.Empty;
}
public class CarDto
{
public int Id { set; get; }
public string Producer { set; get; } = string.Empty;
}
// mapper definition
[Mapper]
public static partial class CarMapper
{
public static partial IQueryable<CarDto> ProjectToDto(this IQueryable<Car> q);
[MapProperty("Manufacturer.Name", "Producer")]
public static partial CarDto ToDto(Car car);
}
// generated mapper implementation
public partial class CarMapper
{
public static partial IQueryable<CarDto> ProjectToDto(IQueryable<Car> q)
{
return System.Linq.Queryable.Select(q, x => new CarDto() { Id = x.Id, Producer = x.Manufacturer.Name });
}
public static partial CarDto ToDto(Car car)
{
var target = new CarDto();
target.Id = car.Id;
target.Producer = x.Manufacturer.Name;
return target;
}
} |
A first preview of this is released in |
Is your feature request related to a problem? Please describe.
AutoMapper supports ProjectTo(this IQueryable) for creating a select expression for EF or other querable providers. Honestly, after this project, its the last reason to use AutoMapper. If this project could create the needed extension method, that would be super useful.
Describe the solution you'd like
Original
Generated
Additional context
https://docs.automapper.org/en/stable/Queryable-Extensions.html
The text was updated successfully, but these errors were encountered: