无法将类型系统 linq IQueryable 隐式转换为系统集合通用列表 [英] Cannot implicitly convert type system linq IQueryable to system collections generic List

查看:29
本文介绍了无法将类型系统 linq IQueryable 隐式转换为系统集合通用列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法从视图模型控制器获取数据到视图,我已经在 LINQpad 中正确检查了查询的工作.但是我缺少可以在视图中显示的查询输出的转换.

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 which can be displayable in View.

我将三个数据模型产品、图像、规范组合到 ViewModel 中,仅具有选定的属性并希望显示相同的内容.

I have three Data Models Products,Images,Specifications which i combined into ViewModel with only selected properties and want to displaythe same.

 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 类

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


    }

注意:我确实研究了堆栈溢出中的相同错误问题,但是无法弄清楚.

Note: I did look into the same error Questions in stack overflow However was not able to figure it out.

错误 CS0266 无法隐式转换类型 'System.Linq.IQueryable<<匿名类型:字符串名称、字符串 Produt_Code、字符串描述、字符串 Image_Description、字符串 Image_Name、字节 [] image1、字符串 Sz_Measurement_Unit、十进制?大小,字符串 Wg_Measurement_Unit,十进制?重量,整数?Price_Set,整数?Price_Sold>>' 到 'OMS.ViewModels.ProductRegistrationViewModelVM'.存在显式转换(您是否缺少演员表?)

Error CS0266 Cannot implicitly convert type 'System.Linq.IQueryable<<anonymous type: string Name, string Produt_Code, string Description, string Image_Description, string Image_Name, byte[] image1, string Sz_Measurement_Unit, decimal? Size, string Wg_Measurement_Unit, decimal? Weight, int? Price_Set, int? Price_Sold>>' to 'OMS.ViewModels.ProductRegistrationViewModelVM'. An explicit conversion exists (are you missing a cast?)

修正:

如下建议将 ProductRegistrationViewModelVM 作为列表项制作,并且 ViewModel 属性也绑定到查询属性.

As Suggested Below ProductRegistrationViewModelVM is made as List item and also ViewModel properties were bound to query properties.

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

推荐答案

扩展方法 .Select 将返回一个 IEnumerable,但您正在尝试分配将它们分配给 ProductRegistrationViewModelVM 类型的业务对象这样的分配是无效的,您可以将 vm 的类型更改为 var 或者您必须使其as List 如果是这样,查询将如下所示:

The extension method .Select will returns a IEnumerable<TResult>, 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<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 类型的对象,因为它是一个相同类型的列表.你也应该做一个小的回报.如下所示;

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

这篇关于无法将类型系统 linq IQueryable 隐式转换为系统集合通用列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆