Cannot implicitly convert type 'ProductOrderDto' to 'System.Collections.Generic.List<ProductOrderDto>

huangapple go评论59阅读模式
英文:

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&lt;Product&gt; Products { get; set; }
        public IEnumerable&lt;Order&gt; Orders { get; set; }
    }


public class EfProductOrderDal : EfEntityRepositoryBase&lt;ProductOrderDto, EtnContext&gt;, IProductOrderDal
    {
        public List&lt;ProductOrderDto&gt; GetProductOrderDetails()
        {
            using (EtnContext context = new EtnContext())
            {
                var model = new ProductOrderDto();

                model.Products = context.Products.Include(i =&gt; i.ProductOrders).ThenInclude(i =&gt; i.Order).ToList();

                model.Orders = context.Orders.Include(i =&gt; i.ProductOrders).ThenInclude(i =&gt; 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&lt;Product&gt; Products { get; set; }
        public IEnumerable&lt;Order&gt; Orders { get; set; }
    }


public class EfProductOrderDal : EfEntityRepositoryBase&lt;ProductOrderDto, EtnContext&gt;, IProductOrderDal
    {
        public List&lt;ProductOrderDto&gt; GetProductOrderDetails()
        {
            using (EtnContext context = new EtnContext())
            {
                var model = new ProductOrderDto();

                model.Products = context.Products.Include(i =&gt; i.ProductOrders).ThenInclude(i =&gt; i.Order).ToList();

                model.Orders = context.Orders.Include(i =&gt; i.ProductOrders).ThenInclude(i =&gt; i.Product).ToList();

                return model;
            }
        }
    }

enter image description here

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;
    }
}

解释: 由于您定义了方法的返回类型为 List,因此您最终需要返回相同类型的列表。但是您定义了单个实例 new 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 和 IEnumerable,所以当您返回 ProductOrderDto 本身时,它将包含产品和订单的集合,因此您可以直接将列表分配给您的 ProductOrderDto 对象,即 Products 和 Orders。

如果您想在以上概念上再投资一些时间,可以在此处查看官方文档。

英文:

> 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&lt;ProductOrderDto&gt; GetProductOrderDetails()
        {
            using (EtnContext context = new EtnContext())
            {
                //As your method signature is List&lt;ProductOrderDto&gt; thus, define your list type
                var model = new List&lt;ProductOrderDto&gt;();
                //Get each collection of products and Orders
                var products = context.Products.Include(i =&gt; i.ProductOrders).ThenInclude(i =&gt; i.Order).ToList();
                var orders = context.Orders.Include(i =&gt; i.ProductOrders).ThenInclude(i =&gt; 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&lt;ProductOrderDto&gt; GetProductOrderDetails()
        {
            using (EtnContext context = new EtnContext())
            {
                var model = new List&lt;ProductOrderDto&gt;();

                model.Add(context.Products.Include(i =&gt; i.ProductOrders).ThenInclude(i =&gt; i.Order).ToList());
                model.Add(context.Orders.Include(i =&gt; i.ProductOrders).ThenInclude(i =&gt; 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 =&gt; i.ProductOrders).ThenInclude(i =&gt; i.Order).ToList();
                model.Orders  = context.Orders.Include(i =&gt; i.ProductOrders).ThenInclude(i =&gt; 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.

huangapple
  • 本文由 发表于 2023年2月16日 08:59:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定