© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
4 replies
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
                }
            };
        }
`

so i have this code, i just refactor pre-existing code to use in multiple methods but, getting error now somehow
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Null Authkey error
C#CC# / help
2y ago
❔ Dereference null error
C#CC# / help
3y ago
Null Reference error but it's not null?
C#CC# / help
3y ago
❔ IOptions pattern in WebAPI getting null value
C#CC# / help
3y ago