Search code examples
c#entity-frameworklinqasp.net-corelambda

Cannot convert lambda expression to type 'object'


How can I get this Lambda expression to work in Entity Framework?

Basically, if there is a goalCyleId, then look it up and get the end date of it.

.Select(x => new GoalDTO()
{
    GoalId = x.GoalId,
    Name = x.Name,
    DueDate = x.GoalCycleId == null ? null : _context.GoalCycles.Find(y => y.GoalCycleId == x.GoalCycleId).EndDate
})

I'm getting an error

Cannot convert lambda expression to type 'object' because it is not a delegate type


Solution

  • Find method of the DbSet class expects a primary key value as its parameter, but you are passing it a lambda expression. Use FirstOrDefault instead._context.GoalCycles.FirstOrDefault(y => y.GoalCycleId == x.GoalCycleId).EndDate