英文:
Cannot implicitly convert type 'ProductOrderDto' to 'System.Collections.Generic.List<ProductOrderDto>
问题
public class ProductOrder
{
public int PId { get; set; }
public Product Product { get; set; }
public int OId { get; set; }
public Order Order { get; set; }
}
public class ProductOrderDto
{
public IEnumerable<Product> Products { get; set; }
public IEnumerable<Order> Orders { get; set; }
}
public class EfProductOrderDal : EfEntityRepositoryBase<ProductOrderDto, EtnContext>, IProductOrderDal
{
public List<ProductOrderDto> GetProductOrderDetails()
{
using (EtnContext context = new EtnContext())
{
var model = new ProductOrderDto();
model.Products = context.Products.Include(i => i.ProductOrders).ThenInclude(i => i.Order).ToList();
model.Orders = context.Orders.Include(i => i.ProductOrders).ThenInclude(i => i.Product).ToList();
return model;
}
}
}
英文:
public class ProductOrder
{
public int PId { get; set; }
public Product Product { get; set; }
public int OId { get; set; }
public Order Order { get; set; }
}
public class ProductOrderDto
{
public IEnumerable<Product> Products { get; set; }
public IEnumerable<Order> Orders { get; set; }
}
public class EfProductOrderDal : EfEntityRepositoryBase<ProductOrderDto, EtnContext>, IProductOrderDal
{
public List<ProductOrderDto> GetProductOrderDetails()
{
using (EtnContext context = new EtnContext())
{
var model = new ProductOrderDto();
model.Products = context.Products.Include(i => i.ProductOrders).ThenInclude(i => i.Order).ToList();
model.Orders = context.Orders.Include(i => i.ProductOrders).ThenInclude(i => i.Product).ToList();
return model;
}
}
}
i would like to perform data insertion and data display of the tables that i relate to many to many, but i am receiving such a mistake
答案1
得分: 0
我想执行表格的数据插入和数据显示,这些表格之间存在多对多的关系,但我一直收到这样的错误。
根据您的情景,您可能想要返回包含产品和订单列表的 ProductOrderDto 或者您可能想要返回包含产品和订单集合的 ProductOrderDto 列表。以下是您可以实现这两种方式的方法:
返回 ProductOrderDto 的集合:
public List<ProductOrderDto> GetProductOrderDetails()
{
using (EtnContext context = new EtnContext())
{
// 因为您的方法签名是 List<ProductOrderDto>,所以定义您的列表类型
var model = new List<ProductOrderDto>();
// 获取每个产品和订单的集合
var products = context.Products.Include(i => i.ProductOrders).ThenInclude(i => i.Order).ToList();
var orders = context.Orders.Include(i => i.ProductOrders).ThenInclude(i => i.Product).ToList();
//将产品和订单绑定到列表中
model.Add(products);
model.Add(orders);
// 现在您可以返回您的列表
return model;
}
}
解释: 由于您定义了方法的返回类型为 Listnew ProductOrderDto();
因此您的返回类型不匹配。因此,如果您想要分配产品和订单的集合,您应该创建 new List<ProductOrderDto>()
,最后,您可以通过 model.Add
将集合分配到其中。您可以在此处查看更多官方文档。
另一种方法:
public List<ProductOrderDto> GetProductOrderDetails()
{
using (EtnContext context = new EtnContext())
{
var model = new List<ProductOrderDto>();
model.Add(context.Products.Include(i => i.ProductOrders).ThenInclude(i => i.Order).ToList());
model.Add(context.Orders.Include(i => i.ProductOrders).ThenInclude(i => i.Product).ToList());
return model;
}
}
返回单个 ProductOrderDto:
public ProductOrderDto GetProductOrderDetails()
{
using (EtnContext context = new EtnContext())
{
var model = new ProductOrderDto();
model.Products = context.Products.Include(i => i.ProductOrders).ThenInclude(i => i.Order).ToList();
model.Orders = context.Orders.Include(i => i.ProductOrders).ThenInclude(i => i.Product).ToList();
return model;
}
}
注意: 因为您的 ProductOrderDto 的类型是 IEnumerable
如果您想在以上概念上再投资一些时间,可以在此处查看官方文档。
英文:
> I would like to perform data insertion and data display of the tables
> that i relate to many to many, but i am receiving such a mistake.
Well, based on your scenario you might want to either return ProductOrderDto which would containing List of Products and Orders or you would like to return List of ProductOrderDto with collection of Products and Orders. So here are the both way how you could implement those:
Return Collection Of ProductOrderDto:
public List<ProductOrderDto> GetProductOrderDetails()
{
using (EtnContext context = new EtnContext())
{
//As your method signature is List<ProductOrderDto> thus, define your list type
var model = new List<ProductOrderDto>();
//Get each collection of products and Orders
var products = context.Products.Include(i => i.ProductOrders).ThenInclude(i => i.Order).ToList();
var orders = context.Orders.Include(i => i.ProductOrders).ThenInclude(i => i.Product).ToList();
//Bind products and Orders Into List
model.Add(products);
model.Add(orders);
// Now You can return your list
return model;
}
}
Explanation: As you have defined your method return type List<ProductOrderDto> so you eventually require to return of type list as well. But you have defined single instance as new ProductOrderDto(); Thus, your return type doesn't match. Therefore, if you would like to assign list of Products and Orders Collection, you ought to create new List<ProductOrderDto>() and then finally, you can assign collection into it by model.Add. . You can get more details in our official document here.
Alternative Way:
public List<ProductOrderDto> GetProductOrderDetails()
{
using (EtnContext context = new EtnContext())
{
var model = new List<ProductOrderDto>();
model.Add(context.Products.Include(i => i.ProductOrders).ThenInclude(i => i.Order).ToList());
model.Add(context.Orders.Include(i => i.ProductOrders).ThenInclude(i => i.Product).ToList());
return model;
}
}
Return Single ProductOrderDto:
public ProductOrderDto GetProductOrderDetails()
{
using (EtnContext context = new EtnContext())
{
var model = new ProductOrderDto();
model.Products = context.Products.Include(i => i.ProductOrders).ThenInclude(i => i.Order).ToList();
model.Orders = context.Orders.Include(i => i.ProductOrders).ThenInclude(i => i.Product).ToList();
return model;
}
}
Note: As your ProductOrderDto has the type of IEnumerable<Product> and IEnumerable<Order> Orders so when you would return ProductOrderDto itself it would be containing the both product and Order collection so that you can directly assign list to your ProductOrderDto object which is Products and Orders.
If you would like to invest few more time on above concept you could have a look on official document here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论