Queryable Extensions .ProjectTo #847
TabrizHabiyev
started this conversation in
General
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
How can I use parameterization in this project?
In this project, we use the IMapFrom interface for Automapper.I have a problem when using .ProjectTo in some places.For example, in some places, I need to show the product price differently depending on the user type .
A more logical and best practice version of these codes is required.
using Application.Common.Interfaces;
using Application.Common.Mappings;
using Application.Common.Models;
using AutoMapper.QueryableExtensions;
using MediatR;
namespace Application.Features
{
public record GetProductsByCategoryQuery : IRequest<PaginatedList>
{
public Guid CategoryId { get; init; }
public int PageNumber { get; init; } = 1;
public int PageSize { get; init; } = 10;
}
}
public class GetProductsByCategoryQueryDto : ProductDto, IMapFrom
{
public string PictureUrl { get; set; } = string.Empty;
public int Discount { get; set; }
public DateTime Created { get; set; }
public ICollection? ProductPrices { get; set; } = new List();
public void Mapping(Profile profile)
{
profile.CreateMap<Product, GetProductsByCategoryQueryDto>()
.ForMember(d => d.ProductPrices, otp => otp.ExplicitExpansion())
.ForMember(d => d.PictureUrl, opt => opt.MapFrom(s =>
StorageManager.GetUrl(s.ProductPhotos.First().ProductFile.Storage,
s.ProductPhotos.First().ProductFile.Path) ?? string.Empty))
.ForMember(x => x.Discount, otp =>
otp.MapFrom(s => s.Discount.EndDate >= DateTime.Now ? s.Discount.DiscountPercentage : 0));
}
}
There are codes in this link that may be useful to me, but I could not use them in this project.
https://docs.automapper.org/en/stable/Queryable-Extensions.html#parameterization
Beta Was this translation helpful? Give feedback.
All reactions