-
Hi, I was trying to filter with Sieve and QueryKit mapping my model to Dto with IQueryable projection like this: var offers = _dbContext.ExchangeOffers.AsNoTracking().ToDto().ApplyQueryKitFilter(filterInput); where ToDto is a projection method: public static partial IQueryable<ExchangeOfferDto> ToDto(this IQueryable<ExchangeOffer> exchangeOffer); But If I use the code generated by Mapperly I get: Upon further investigation, I found that the issue can be traced back to the null-check for the collection property, specifically: x.WantedPublishedBooks != null ? ... :default
and
x.OfferedBooks != null ? ... : default Mapperly checks for null by default and i can't find a way to change this behavior. When I manually write the mapping to the DTO without this null-check, everything functions correctly. So to summarize this manual projection works and i can filter it correctly: public static IQueryable<ExchangeOfferDto> ToDto(this IQueryable<ExchangeOffer> exchangeOffer)
{
return Queryable.Select(exchangeOffer, x => new ExchangeOfferDto()
{
Id = x.Id,
Points = x.Points,
DateCreated = x.DateCreated,
Status = x.Status,
OfferedBooks = Enumerable.ToList(Enumerable.Select(x.OfferedBooks, x1 => new UserBookDto()
{
Id = x1.Id,
UserId = x1.UserId,
PublishedBookId = x1.PublishedBookId,
IsAvailable = x1.IsAvailable,
})),
WantedPublishedBooks = Enumerable.ToList(Enumerable.Select(x.WantedPublishedBooks, x1 => new DetailedPublishedBookDto()
{
Id = x1.Id,
ISBN = x1.ISBN,
Title = x1.GeneralBook.Title,
CoverImageURL = x1.CoverImageURL,
PublicationYear = x1.PublicationYear,
Publisher = x1.Publisher,
Author = x1.GeneralBook.Author,
}))
});
} And this one causes the error - the only difference is the public static IQueryable<ExchangeOfferDto> ToDto(this IQueryable<ExchangeOffer> exchangeOffer)
{
return Queryable.Select(exchangeOffer, x => new ExchangeOfferDto()
{
Id = x.Id,
Points = x.Points,
DateCreated = x.DateCreated,
Status = x.Status,
OfferedBooks = x.OfferedBooks != null ? Enumerable.ToList(Enumerable.Select(x.OfferedBooks, x1 => new UserBookDto()
{
Id = x1.Id,
UserId = x1.UserId,
PublishedBookId = x1.PublishedBookId,
IsAvailable = x1.IsAvailable,
})) : default,
WantedPublishedBooks = x.WantedPublishedBooks != null ? Enumerable.ToList(Enumerable.Select(x.WantedPublishedBooks, x1 => new DetailedPublishedBookDto()
{
Id = x1.Id,
ISBN = x1.ISBN,
Title = x1.GeneralBook.Title,
CoverImageURL = x1.CoverImageURL,
PublicationYear = x1.PublicationYear,
Publisher = x1.Publisher,
Author = x1.GeneralBook.Author,
})) : default
});
} Any ideas why that might be causing issues? Is it possible for Mapperly to avoid generating such null-checks for collection properties ? My Dto: public class ExchangeOfferDto
{
public int Id { get; set; }
public int Points { get; set; }
public DateTime DateCreated { get; set; }
public ExchangeOfferStatus Status { get; set; }
public UserDto? User { get; set; }
public ICollection<UserBookDto>? OfferedBooks { get; set; }
public ICollection<DetailedPublishedBookDto>? WantedPublishedBooks { get; set; }
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Are your objects in a disabled nullable context? |
Beta Was this translation helpful? Give feedback.
Nah I think you are good, the problem here is that since these properties are in a nullable disabled context, Mapperly assumes they are nullable and generates the not-null condition. EF core seems to fail to translate non-null conditions on collections (which don‘t make sense from an EF core point of view). As a workaround you can probably enable nullability for the collection properties and set them to non-nullable. I‘ll create an issue that Mapperly should not generate this non-null condition in projection mappings for collections.