C# 通过加入获取类别中的产品列表
我希望在此操作方法中使用查询获得产品列表,然后使用AutoMapper将其映射到C# 通过加入获取类别中的产品列表,c#,linq,automapper,entity-framework-core,C#,Linq,Automapper,Entity Framework Core,我希望在此操作方法中使用查询获得产品列表,然后使用AutoMapper将其映射到ViewModelProduct的IEnumerable: private async Task<IEnumerable<ViewModelProduct>> GetProductsAsync(int id) { if (id == 0) { return null; } else { var category = _co
ViewModelProduct
的IEnumerable
:
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;
}
}
}
专用异步任务GetProductsAsync(int-id)
{
如果(id==0)
{
返回null;
}
其他的
{
var category=\u context.ProductCategories.Where(p=>p.Id==Id.FirstOrDefault();
如果(类别!=null)
{
var products=WAIT(来自_context.ProductsInCategories中的c)
在_context.Products中加入p
关于c.ProductId等于p.Id
其中c.ProductCategoryId==category.Id
选择p).ToListAsync();
var VMProducts=_mapper.Map(products);
退货;
}
其他的
{
返回null;
}
}
}
模型如下所示:
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; }
}
CreateMap<Product, ViewModelProduct>();
公共类产品
{
公共int Id{get;set;}
公共字符串标题{get;set;}
公共字符串信息{get;set;}
公共十进制价格{get;set;}
公共列表信息页面{get;set;}
类别{get;set;}中的公共列表
}
公共类产品分类
{
公共int Id{get;set;}
public int ProductId{get;set;}
公共int排序器{get;set;}
public int ProductCategoryId{get;set;}
公共产品产品{get;set;}
公共产品类别ProductCategory{get;set;}
公共FrontPageProduct FrontPageProduct{get;set;}
}
公共类ViewModelProduct
{
公共int Id{get;set;}
公共字符串标题{get;set;}
公共字符串信息{get;set;}
公共十进制价格{get;set;}
公共int排序器{get;set;}
公共IEnumerable InFrontPages{get;set;}
公共IEnumerable类别{get;set;}
}
我的映射配置文件如下所示:
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; }
}
CreateMap<Product, ViewModelProduct>();
CreateMap();
但是,我从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;
}
}
}
ProductInCategory->ViewModelProduct(目标成员列表)
MyStore.Models.ProductInCategory->MyStore.Models.ViewModels.ViewModelProduct(目标成员列表)
未映射属性:
头衔
信息
价格
信息页
类别
AutoMapper.ConfigurationValidator.AssertConfiguration验证ID(IEnumerable typeMaps)
AutoMappingException:映射类型错误
映射类型:
列表1->IEnumerable
1
System.Collections.Generic.List1[[MyStore.Models.ProductInCategory,MyStore,Version=1.0.0,Culture=neutral,PublicKeyToken=null]->System.Collections.Generic.IEnumerable
1[[MyStore.Models.ViewModelProduct,MyStore,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]]
我是否缺少映射,或者我的查询已关闭?异常显示它正试图将
ProductInCategory
列表强制转换到ViewModel中,而不是Product
如评论中所述,将var
更改为List
可以解决问题。没有看到您的确切设置,我不知道为什么选择了错误的类型,但这解决了隐式铸造问题
很高兴修好了!:) 异常本身是因为它试图将ProductInCategory列表映射到ViewModelProduct列表。当产品类型到达映射行时,请验证它实际上是列表。只有当我的源列表类型为ProductInCategory时,我才能复制您的错误。但是,看看您提供的LINQ语句,它似乎应该返回正确的类型,所以我有点困惑为什么会出现这个错误。是否有其他与此相关的代码?这是代码吗?ScottBaldric我将更新我的问题以显示整个操作方法,而不仅仅是查询和映射部分。我';我不在电脑前,所以我不能运行代码,但没有什么会突然出现问题。为了完整性,您可以将var更改为ListProduct吗?它';也有可能是你的地图绘制者正在提取我没有的数据';自从我39岁以后就没有了;不要使用完全含水的物体。是否为ViewModelFrontPageProduct和ViewModelCategoryWithTitle设置了映射?异常意味着源列表错误,但它可能位于其中一个嵌套模型上。@ScottBaldric Yes!将
var
更改为List
成功了!:)谢谢把它作为答案贴出来,我会接受的。:)令人惊叹的。很高兴它起作用了!:)