Doing Left Join with 3 tables using EF Core - multiple on condition

Kalpana 291 Reputation points
2023-10-09T07:18:21.79+00:00

Hi,

This is my sql query.

SELECT  [ID],[MachineName], b.Target_Date, b.Job_Name1, c.StopReasonStart, c.StopReasonEnd
  FROM [OEEDashboard].[dbo].[Machine] a
  left join AllMachines b
    on a.ID = b.MachineID 
    left join OeeDetailsAll c
	on a.ID =c.OeeMachine and b.Target_Date = CAST(c.StopReasonStart as date)
  where b.Target_Date = '2023-10-05' 
  order by a.ID, c.StopReasonStart

How can I get the ef core to return this query, I tried this.

 var emp = from a in _dbContext.Machines
                      join b in _dbContext.AllMachines
                      on  a.Id equals b.MachineId 
                      join c in _dbContext.OeeDetailsAlls
                      on a.Id equals c.OeeMachine
                      
                      select new 
                      {
                         EntA = a, EntB = b
                      };

but I am a bit lost on including the last ON condition in it . This specific line into the ef core query. Kindly help.

	on a.ID =c.OeeMachine and b.Target_Date = CAST(c.StopReasonStart as date)
		
Developer technologies .NET Entity Framework Core
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2023-10-09T08:15:47.77+00:00

    Check if this works: on a.Id equals c.OeeMachine where b.Target_Date == c.StopReasonStart.Date.

    Or maybe: on new { a.Id, b.Target_Date } equals new { Id = c.OeeMachine, Target_Date = c.StopReasonStart.Date }.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.