参数类型不匹配,DateTime和DateTime

时间:2018-11-16 11:48:24

标签: c# linq entity-framework-core

抛出异常参数类型不匹配,尽管它们都是DateTime。

引发异常
CreatedAt = db.TransactionLogs
     .Last(x => x.TransactionId == a.Id)                                    
     .CreatedAt

如果我改用CreatedAt = DateTime.Now(),则查询工作正常

TransactionLogs.CreatedAt不可为空,我不明白这是什么问题。

public IList<HeadlineDisplayModel> GetTeamApplicationHeadlines(string userId, int year)
        {
            using (var db = new TrainingManagerDbContext(Options))
            {
                return (from a in db.Transactions
                        join o in db.Options on a.OptionId equals o.Id
                        join e in db.Employees on a.EmployeeId equals e.Id
                        join s in db.TransactionStatuses on a.TransactionStatusId equals s.Id
                        where
                            a.Employee.DirectManagerId == userId
                            && a.TransactionStatus.Id != TransactionStatus.DRAFT
                            && !a.IsDeleted
                            && a.BudgetYear == year
                        select new HeadlineDisplayModel
                        {
                            Title = o.Title,
                            TransactionId = a.Id,
                            DateFrom = a.PeriodFrom,
                            Price = a.Price,
                            StatusName = s.Name,
                            EmployeeFullName = e.FullName,
                            CreatedAt = db.TransactionLogs.Last(x => x.TransactionId == a.Id)
                                .CreatedAt
                        }).OrderByDescending(x => x.CreatedAt).ToList();
            }
        }

HeadlineDisplayModel

public DateTime CreatedAt { get; set; }

TransactionLog

[Required]
[Column("created_at", TypeName = "datetime")]
public DateTime CreatedAt { get; set; }

enter image description here

    USE [KanbanBoard]
GO

/****** Object:  Table [tm].[TransactionLogs]    Script Date: 16.11.2018 14:49:57 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [tm].[TransactionLogs](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [transaction_id] [int] NOT NULL,
    [price] [decimal](18, 2) NULL,
    [transaction_status_id] [varchar](8) NULL,
    [budget_year] [int] NULL,
    [additional_price] [decimal](18, 2) NOT NULL,
    [period_from] [datetime] NOT NULL,
    [period_to] [datetime] NOT NULL,
    [comment] [nvarchar](max) NULL,
    [created_by] [varchar](70) NULL,
    [created_at] [datetime] NOT NULL,
    [total_hours] [int] NOT NULL,
 CONSTRAINT [PK_TransactionLogs] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [tm].[TransactionLogs] ADD  DEFAULT ((0)) FOR [total_hours]
GO

ALTER TABLE [tm].[TransactionLogs]  WITH CHECK ADD  CONSTRAINT [FK_TransactionLogs_Employees_created_by] FOREIGN KEY([created_by])
REFERENCES [tm].[Employees] ([id])
GO

ALTER TABLE [tm].[TransactionLogs] CHECK CONSTRAINT [FK_TransactionLogs_Employees_created_by]
GO

ALTER TABLE [tm].[TransactionLogs]  WITH CHECK ADD  CONSTRAINT [FK_TransactionLogs_Transactions_transaction_id] FOREIGN KEY([transaction_id])
REFERENCES [tm].[Transactions] ([id])
ON DELETE CASCADE
GO

ALTER TABLE [tm].[TransactionLogs] CHECK CONSTRAINT [FK_TransactionLogs_Transactions_transaction_id]
GO

ALTER TABLE [tm].[TransactionLogs]  WITH CHECK ADD  CONSTRAINT [FK_TransactionLogs_TransactionStatuses_transaction_status_id] FOREIGN KEY([transaction_status_id])
REFERENCES [tm].[TransactionStatuses] ([id])
GO

ALTER TABLE [tm].[TransactionLogs] CHECK CONSTRAINT [FK_TransactionLogs_TransactionStatuses_transaction_status_id]
GO

1 个答案:

答案 0 :(得分:1)

db.TransactionLogs.OrderByDescending(x=> x.Id)
       .First(x => x.TransactionId == a.Id)
       .CreatedAt

Linq Last出了点问题,所以我相反订购了它,并使用了First。现在一切都很好。 :)