Stian
Stian

Reputation: 1602

Getting a list of products in a category via join

I'm expecting to get a list of products using the query in this action method, and then map it to an IEnumerable of ViewModelProduct with AutoMapper:

private async Task<IEnumerable<ViewModelProduct>> GetProductsAsync(int id)
{
    if (id == 0)
    {
        return null;
    }
    else
    {
        var category = _context.ProductCategories.Where(p => p.Id == id).FirstOrDefault();
        if (category != null)
        {
            var products = await (from c in _context.ProductsInCategories
                                  join p in _context.Products
                                  on c.ProductId equals p.Id
                                  where c.ProductCategoryId == category.Id
                                  select p).ToListAsync();
            var VMProducts = _mapper.Map<IEnumerable<ViewModelProduct>>(products);
            return VMProducts;
        }
        else
        {
            return null;
        }
    }
}

The models look like this:

public class Product
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Info { get; set; }
    public decimal Price { get; set; }
    public List<FrontPageProduct> InFrontPages { get; set; }
    public List<ProductInCategory> InCategories { get; set; }
}

public class ProductInCategory
{
    public int Id { get; set; }
    public int ProductId { get; set; }
    public int SortOrder { get; set; }
    public int ProductCategoryId { get; set; }
    public Product Product { get; set; }
    public ProductCategory ProductCategory { get; set; }
    public FrontPageProduct FrontPageProduct { get; set; }
}

public class ViewModelProduct
{
    public int Id { get; set; }
    public string Title { get; set; }
    public string Info { get; set; }
    public decimal Price { get; set; }
    public int SortOrder { get; set; }
    public IEnumerable<ViewModelFrontPageProduct> InFrontPages { get; set; }
    public IEnumerable<ViewModelCategoryWithTitle> Categories { get; set; }
}

My mapping profile looks like this:

CreateMap<Product, ViewModelProduct>();

However, I get this message from AutoMapper:

ProductInCategory -> ViewModelProduct (Destination member list) MyStore.Models.ProductInCategory -> MyStore.Models.ViewModels.ViewModelProduct (Destination member list)

Unmapped properties:

Title

Info

Price

InFrontPages

Categories

AutoMapper.ConfigurationValidator.AssertConfigurationIsValid(IEnumerable typeMaps)

AutoMapperMappingException: Error mapping types.

Mapping types: List1 -> IEnumerable1 System.Collections.Generic.List1[[MyStore.Models.ProductInCategory, MyStore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.IEnumerable1[[MyStore.Models.ViewModels.ViewModelProduct, MyStore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

Am I missing a mapping, or is my query off?

Upvotes: 1

Views: 309

Answers (1)

Scott Baldric
Scott Baldric

Reputation: 465

The exception is showing that it's trying to cast a list of ProductInCategery into the ViewModel instead of Product.

As mentioned in the comments, changing the var to List<Product> fixes the problem. Without seeing your exact setup, I can't know why the wrong type was being chosen, but this susses out the implicit casting problem.

Glad it got fixed! :)

Upvotes: 1

Related Questions