繁体   English   中英

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

[英]Cannot implicitly convert type 'System.Collections.Generic.List error

有谁知道为什么尝试通过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类结合起来

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

那么,为什么不将其声明为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();

很明显。

限定:

IEnumerable<ForestLogisticBEAN> _ForestLogisticsBeans;

要么

List<ForestLogisticBEAN> _ForestLogisticsBeans;

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

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

或使用

IEnumerable<ForestLogisticBEAN> _ForestLogisticsBeans;

要么

List<ForestLogisticBEAN> _ForestLogisticsBeans;

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM