无法将类型系统linq IQueryable隐式转换为系统集合通用List

时间:2017-01-27 00:30:48

标签: c# asp.net linq asp.net-mvc-5 razorengine

无法从View Model Controller获取数据到View,我已经正确检查了LINQpad中的查询工作。但是我错过了可以在View中显示的查询输出的转换。

我有三个数据模型产品,图像,规格,我只与选定的属性合并到ViewModel中,并希望显示相同的内容。

 public class ViewModelController : Controller
    {
        private OMSEntities db = new OMSEntities();
        // GET: ViewModel
        public ActionResult Index()
        {
            var product = new Product();
            var prices = new Price();
            var orders = new Order();
           ProductRegistrationViewModelVM  vm = new ProductRegistrationViewModelVM();

           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
                       {
                           //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
                       });

          vm.Add(new ProductRegistrationViewModelVM());

            return View(vm.ToList());
        }

ViewModel Class

 public class ProductRegistrationViewModelVM
    {
        //Products
        public int ProductId { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public string Produt_Code { get; set; }
        public Nullable<int> Supplier_Id { get; set; }
        public IEnumerable<Supplier> Supplier { get; set; }
        //Image
        public string Image_Description { get; set; }
        public string Image_Name { get; set; }
        public byte[] image1 { get; set; }

        //Specifications

        public string Sz_Measurement_Unit { get; set; }
        public Nullable<decimal> Size { get; set; }
        public string Size_Name { get; set; }
        public string Wg_Measurement_Unit { get; set; }
        public Nullable<decimal> Weight { get; set; }

        public Nullable<int> Price_Set { get; set; }
        public Nullable<int> Price_Sold { get; set; }


    }

注意:我确实在堆栈溢出中查找了同样的错误问题但是无法弄明白。

  

错误CS0266无法隐式转换类型'System.Linq.IQueryable&lt;&lt;匿名类型:字符串名称,字符串Produt_Code,字符串说明,字符串Image_Description,字符串Image_Name,byte [] image1,字符串Sz_Measurement_Unit,decimal?大小,字符串Wg_Measurement_Unit,十进制?重量,int? Price_Set,int? Price_Sold&GT;&GT;”到'OMS.ViewModels.ProductRegistrationViewModelVM'。存在显式转换(您是否错过了演员?)

修正:

如下所示,将ProductRegistrationViewModelVM作为List项,并将ViewModel属性绑定到查询属性。

 public ActionResult Index()
        {
            var product = new Product();
            var prices = new Price();
            var orders = new Order();
            List<ProductRegistrationViewModelVM> vm = new List<ProductRegistrationViewModelVM>();
           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
                       Name=p.Name,
                       Produt_Code=   p.Produt_Code,
                       Description = p.Description,
                            //Image
                            Image_Description=  i.Image_Description,
                            Image_Name= i.Image_Name,
                            image1= i.image1,
                       //Specifications
                       Sz_Measurement_Unit= s.Sz_Measurement_Unit,
                            Size= s.Size,
                            Wg_Measurement_Unit=s.Wg_Measurement_Unit,
                            Weight=s.Weight,
                            Price_Set=s.Price_Set,
                            Price_Sold= s.Price_Sold
                   }).ToList();

            return View(vm);
        }

2 个答案:

答案 0 :(得分:1)

扩展方法.Select将返回IEnumerable<TResult>,但您尝试将它们分配给类型为ProductRegistrationViewModelVM的业务对象,此类分配有效,您可以更改类型vmvar或您必须将其设为列表,如果是,则查询将如下所示:

List<ProductRegistrationViewModelVM> 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();

现在您可以轻松地向其添加另一个ProductRegistrationViewModelVM类型的对象,因为它是一个相同类型的List。你也应该做一些小改变。这将是如下;

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

答案 1 :(得分:0)

你创建一个这样的虚拟机:

ProductRegistrationViewModelVM  vm = new ProductRegistrationViewModelVM();

然后指定一个IQueryable,它返回匿名类型。那不会过去。然后,您尝试向其添加视图模型的新实例,这也不起作用:

vm.Add(new ProductRegistrationViewModelVM());

<强>修正

请改为尝试:

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

return View( vm.ToList() );

并确保您的视图接受模型为List<ProductRegistrationViewModelVM>