fromliberty
getting null error
public async Task<IEnumerable<ProductDto>> GetProductsByLabel(string labelName)
{
// this will get products based on label, it will be useful to filter based on label.
var productsQuery = await _db.Products
.Where(p => p.Labels.Any(l => l.Name == labelName)).ToListAsync();
var products = MapProductsToDto(productsQuery);
return _mapper.Map<List<ProductDto>>(products);
}
private static List<ProductDto> MapProductsToDto(IEnumerable<Product> products)
{
return products.Select(MapProductToDto).ToList();
}
private static ProductDto MapProductToDto(Product p)
{
return new ProductDto
{
ProductId = p.ProductId,
Name = p.Name,
CategoryName = p.CategoryName,
ImageUrl = p.ImageUrl,
Description = p.Description,
HoverImageUrl = p.HoverImageUrl,
Colours = p.Colours.Select(c => new ColourDto
{
Id = c.Id,
Name = c.Name,
Img = c.Img,
Quantity = c.Quantity
}).ToList(),
Labels = p.Labels.Select(l => new LabelDto
{
Id = l.LabelId,
Name = l.Name
}).ToList(),
Price = p.Price,
Rating = new RatingDto
{
Count = p.Rating.Count,
Rate = p.Rating.Rate
}
};
}
public async Task<IEnumerable<ProductDto>> GetProductsByLabel(string labelName)
{
// this will get products based on label, it will be useful to filter based on label.
var productsQuery = await _db.Products
.Where(p => p.Labels.Any(l => l.Name == labelName)).ToListAsync();
var products = MapProductsToDto(productsQuery);
return _mapper.Map<List<ProductDto>>(products);
}
private static List<ProductDto> MapProductsToDto(IEnumerable<Product> products)
{
return products.Select(MapProductToDto).ToList();
}
private static ProductDto MapProductToDto(Product p)
{
return new ProductDto
{
ProductId = p.ProductId,
Name = p.Name,
CategoryName = p.CategoryName,
ImageUrl = p.ImageUrl,
Description = p.Description,
HoverImageUrl = p.HoverImageUrl,
Colours = p.Colours.Select(c => new ColourDto
{
Id = c.Id,
Name = c.Name,
Img = c.Img,
Quantity = c.Quantity
}).ToList(),
Labels = p.Labels.Select(l => new LabelDto
{
Id = l.LabelId,
Name = l.Name
}).ToList(),
Price = p.Price,
Rating = new RatingDto
{
Count = p.Rating.Count,
Rate = p.Rating.Rate
}
};
}
5 replies