无法隐式转换类型System.Collections.Generic.List错误

时间:2015-03-12 13:16:20

标签: c# asp.net-mvc

有没有人知道为什么我在尝试通过ForestLogisticBEAN类编辑数据库中的内容时会出现此错误?

  

无法隐式转换类型   ' System.Collections.Generic.List to   ' System.Linq.IQueryable&#39 ;.   存在显式转换(您是否错过了演员?)

这是集成层:

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,但现在却抛出了这个错误

  

传递到字典中的模型项是类型的   &#39; System.Collections.Generic.List`1 [ForestLogistics.Data.BEANS.ForestLogisticBEAN]&#39 ;,   但是这个字典需要一个类型的模型项   &#39; ForestLogistics.Data.Order&#39;

这是新的集成层:

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类

组合在一起

4 个答案:

答案 0 :(得分:2)

Enumerable.ToList返回List<T>,其中IEnumerable<T>而不是IQueryable<T>。因此,您无法将其分配给IQueryable<ForestLogisticBEAN>变量。

那你为什么不把它声明为列表或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();

答案 1 :(得分:0)

很清楚。

定义:

IEnumerable<ForestLogisticBEAN> _ForestLogisticsBeans;

List<ForestLogisticBEAN> _ForestLogisticsBeans;

答案 2 :(得分:0)

为什么要将_ForestLogisticsBeans定义为IQueryable<ForestLogisticBEAN>?您已将其定义为IQueryable并且您正在分配List它总是会给您错误。

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

或使用

IEnumerable<ForestLogisticBEAN> _ForestLogisticsBeans;

或者

List<ForestLogisticBEAN> _ForestLogisticsBeans;

答案 3 :(得分:0)

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