Skip to main content

IQueryable projections

Mapperly does support IQueryable<T> projections:

[Mapper]
public static partial class CarMapper
{
public static partial IQueryable<CarDto> ProjectToDto(this IQueryable<Car> q);
}

This is useful in combination with Entity Framework and other ORM solutions which expose IQueryable<T>. Only fields present in the target class will be retrieved from the database.

info

Since queryable projection mappings use System.Linq.Expressions.Expression<T> under the hood, such mappings have several limitations:

  • Object factories are not applied
  • Constructors with unmatched optional parameters are ignored
  • ThrowOnPropertyMappingNullMismatch is ignored
  • AllowNullPropertyAssignment is ignored
  • Enum mappings do not support the ByName strategy
  • Reference handling is not supported
  • Nullable reference types are disabled
  • User implemented mapping methods need to follow expression tree limitations.

Property configurations

To configure property mappings add partial mapping method definitions with attributes as needed. Set these methods to private to hide them from callers.

[Mapper]
public static partial class CarMapper
{
public static partial IQueryable<CarDto> ProjectToDto(this IQueryable<Car> q);

[MapProperty(nameof(Car.Manufacturer), nameof(CarDto.Producer)]
private static partial CarDto Map(Car car);
}