Cannot implicitly convert type system linq IQueryable to system collections generic List

前端 未结 2 804
青春惊慌失措
青春惊慌失措 2021-01-25 10:49

Unable to fetch Data from View Model Controller to View,i have properly checked the working of query in LINQpad. However I am missing the conversion of the output of query whic

2条回答
  •  醉话见心
    2021-01-25 11:14

    The extension method .Select will returns a IEnumerable, But you are trying to assign them to a business object of type ProductRegistrationViewModelVM such assignment is invalid, either you can change the type of vm to var or you have to make it as List if so the query will be like the following:

    List vm =(from p in db.Products
                       join i in db.Images on p.ProductId equals i.Product_Id
                       join s in db.Specifications on p.ProductId equals s.Product_Id
                       select new ProductRegistrationViewModelVM
                       {
                           //Product
                           p.Name,
                           p.Produt_Code,
                           p.Description,
                           //Image
                           i.Image_Description,
                           i.Image_Name,
                           i.image1,
                           //Specifications
                           s.Sz_Measurement_Unit,
                           s.Size,
                           s.Wg_Measurement_Unit,
                           s.Weight,
                           s.Price_Set,
                           s.Price_Sold
                       }).ToList();
    

    Now you can easily add another object of type ProductRegistrationViewModelVM to this since it a List of the same type. And you should do a small change in return as well. which will be like the following;

    return View(vm); // No ToList is needed here since it is already a list
    

提交回复
热议问题