Argument types do not match, DateTime and DateTime

c# entity-framework-core linq

Question

Exception Argument types do not match is thrown, although both of them are DateTime. Exception is thrown at

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

Query works just fine if I put CreatedAt = DateTime.Now() instead

TransactionLogs.CreatedAt is not nullable and I don't understand what's the issue.

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
1
11/16/2018 12:51:44 PM

Accepted Answer

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

Something's wrong with Linq Last, so I ordered it in opposite and used First. Everything is okey now. :)

2
11/16/2018 12:31:54 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow