【问题标题】:Cannot implicitly convert type 'System.Collections.Generic.List error无法隐式转换类型'System.Collections.Generic.List 错误
【发布时间】:2015-03-12 13:16:20
【问题描述】:

有人知道为什么我在尝试通过 ForestLogisticBEAN 类编辑数据库中的某些内容时收到此错误吗?

不能隐式转换类型 'System.Collections.Generic.List 到 'System.Linq.IQueryable'。 存在显式转换(您是否缺少演员表?)

这里是集成层:

public void EditParcelDetail(ForestLogisticBEAN parcel){

    IQueryable<ForestLogisticBEAN> _ForestLogisticsBeans;

    _ForestLogisticsBeans = (from order in _context.Orders
              from tracking in _context.Trackings
              where order.CustomerID == parcel.Id
              where tracking.CustomerId == parcel.Id
              select new {order.DeliveryDate, order.OrderDate, tracking.Status} ).ToList();
      }

这是 ForestLogisticBEAN 类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ForestLogistic.Data.BEANS
{
    public class ForestLogisticBEAN
    {

        public int TrackingId { get; set; }
        public int OrderId { get; set; }
        public bool Status { get; set; }
        public DateTime OrderDate { get; set; }
        public DateTime DeliveryDate { get; set; }

        public ForestLogisticBEAN() { }
    }
}

更新: 我将其更改为 List 而不是 IQueryable 但现在它正在抛出此错误

传入字典的模型项是类型 'System.Collections.Generic.List`1[ForestLogistics.Data.BEANS.ForestLogisticBEAN]', 但是这本字典需要一个类型的模型项 'ForestLogistics.Data.Order'。

这是新的集成层:

public void EditParcelDetail(ForestLogisticBEAN parcel)

        {

                List<ForestLogisticBEAN> _ForestLogisticsBeans;

              _ForestLogisticsBeans = (from order in _context.Orders
                                from tracking in _context.Trackings

                                 where order.CustomerID == parcel.Id
                                where tracking.CustomerId == parcel.Id
                                select new ForestLogisticBEAN {
                                    DeliveryDate = parcel.DeliveryDate,
                                    OrderDate = parcel.OrderDate,
                                    Status = parcel.Status

                                });

              _context.SaveChanges();

          }

目的是能够将订单和跟踪表与bean类结合起来

【问题讨论】:

  • 为什么声明为IQueryable&lt;T&gt;而不是List&lt;T&gt;IEnumerable&lt;T&gt;
  • 为什么不使用var? var _ForestLogisticsBeans = (from order in _context.Orders....

标签: c# asp.net-mvc


【解决方案1】:

Enumerable.ToList 返回一个实现IEnumerable&lt;T&gt; 而不是IQueryable&lt;T&gt;List&lt;T&gt;。所以你不能将它分配给IQueryable&lt;ForestLogisticBEAN&gt; 变量。

那你为什么不把它声明为list或者IEnumerable呢?

IEnumerable<ForestLogisticBEAN> _ForestLogisticsBeans;

您还必须创建ForestLogisticBEAN 的实例,而不是您选择的任何匿名类型。例如:

_ForestLogisticsBeans = (from order in _context.Orders
                         from tracking in _context.Trackings
                         where order.CustomerID == parcel.Id
                         where tracking.CustomerId == parcel.Id
                         select new ForestLogisticBEAN { 
                             Status  = tracking.Status,
                             OrderDate = order.OrderDate,
                             DeliveryDate = order.DeliveryDate
                         }).ToList();

【讨论】:

  • 我用你的建议更新了它,但现在我收到另一个错误'传递到字典的模型项的类型是'System.Collections.Generic.List`1[ForestLogistics.Data.BEANS. ForestLogisticBEAN]',但此字典需要“ForestLogistics.Data.Order”类型的模型项。'
  • @RonS:我不熟悉 MVC。但似乎您为模型使用了错误的类型。所以不要使用BEANS.ForestLogisticBEAN‌,而是使用Data.Order
【解决方案2】:

很清楚。

定义:

IEnumerable<ForestLogisticBEAN> _ForestLogisticsBeans;

List<ForestLogisticBEAN> _ForestLogisticsBeans;

【讨论】:

    【解决方案3】:

    为什么将_ForestLogisticsBeans 定义为IQueryable&lt;ForestLogisticBEAN&gt;?您已将其定义为 IQueryable 并且您正在分配 List 它总是给您错误。

    试试var _ForestLogisticsBeans = (from order in _context.Orders....

    或者使用

    IEnumerable<ForestLogisticBEAN> _ForestLogisticsBeans;
    

    或者

    List<ForestLogisticBEAN> _ForestLogisticsBeans;
    

    【讨论】:

      【解决方案4】:

      C# 和 Angular 2+ 中的项目 我相信这会产生结果,这可能对其他人有帮助,同时返回我得到的价值

      “System.Collections.Generic.List”到“System.Collections.Generic.List”

      我的代码是:

              List<Ticket> objTicket = new List<Ticket>();
              objTicket = ticketRepository.GetAllData().ToList();
      
              List<AccountType> ListAccounType = new List<AccountType>();
              ListAccounType = accountTypeRepository.GetAllData().ToList();
      
              List<AccountTransaction> ListAccount = new List<AccountTransaction>();
              ListAccount = accountTranastionRepository.GetAllData().ToList();
      
              List<AccountTransactionValue> ListAccountTransactionValue = new List<AccountTransactionValue>();
              ListAccountTransactionValue = accountTransactionValueRepository.GetAllData().ToList().Where(o => o.Date >= FromDate && o.Date <= ToDate).ToList();
      
              var ListAccountTransaction = from v in ListAccountTransactionValue
                                           join atr in objTicket on v.AccountTransactionId equals atr.Id
                                           join a in ListAccount on v.AccountId equals a.Id
                                           select new { v.Id, Name = a.Name, v.AccountTransactionId, VoucherNo = atr.Name.Substring(atr.Name.IndexOf("[")), AccountTransactionType = atr.Name.Substring(0, atr.Name.IndexOf("[")), atr.Date, SourceAccountTypeId = atr.Name, Description = atr.CompanyCode, DebitAmount = v.Debit, CreditAmount = v.Credit, atr.DepartmentId };
      
              var ListScreenTicketType = ListAccountTransaction.Select(o => new { o.Id }).Distinct().ToList();
      
              AccountTransactionDocument objAccountTransactionDocument = new AccountTransactionDocument();
              if (objAccountTransactionDocument != null)
              {
                  AccountTransactionType objAccountTransactionType = new AccountTransactionType();
              }
      
              List<ScreenTicket> listScreenTicket = new List<ScreenTicket>();
              foreach (var accounttransaction in ListScreenTicketType)
              {
                  var objaccounttransaction = ListAccountTransaction.Where(o => o.Id == accounttransaction.Id).FirstOrDefault();
                  ScreenTicket objScreenTicket = new ScreenTicket();
                  objScreenTicket.Id = accounttransaction.Id;
                  objScreenTicket.Name = objaccounttransaction.Date.ToShortDateString();
                  objScreenTicket.Note = objaccounttransaction.Name;
                  var dataBytes = objaccounttransaction.Name;
                  if (dataBytes != null)
                  {
                      objScreenTicket.IsActive = true;
                  }
                  else
                  {
                      objScreenTicket.IsActive = false;
                  }
                  var objscreens = ListAccountTransaction.Where(o => o.Name == objaccounttransaction.Name);
                  List<PaymentHistory> listPaymentHistory = new List<PaymentHistory>();
                  foreach (var screenvalue in objscreens)
                  {
                      PaymentHistory objPaymentHistory = new PaymentHistory();
                      objPaymentHistory.Id = screenvalue.Id;
                      objPaymentHistory.AmountPaid = screenvalue.CreditAmount;
                      objPaymentHistory.Id = screenvalue.Id;
                      listPaymentHistory.Add(objPaymentHistory);
                  }
                  objScreenTicket.PaymentHistory = listPaymentHistory;
                  listScreenTicket.Add(objScreenTicket);
              }
              return listScreenTicket;
          }
      

      【讨论】:

        猜你喜欢
        • 2015-12-07
        • 1970-01-01
        • 1970-01-01
        • 2020-11-24
        • 2017-10-06
        • 1970-01-01
        • 1970-01-01
        • 2021-11-22
        • 1970-01-01
        相关资源
        最近更新 更多