Translate my SQL to LINQ C# Async : I am trying to convert my SQL code into c# code. This is just a snip of my code as the actual code is too long. 
My SQL 
    Declare @ReportDate DateTime = NULL
    SELECT DISTINCT LotId, PenId, SUM(Head) AS TotalHead
    		INTO #LotPenAlloctionListTempTable
    			FROM (
    					SELECT  lp.LotId, lp.PenId, lp.HeadCountInPen  AS Head
    						FROM scoFeedlot.LotPen AS lp	
    				
    					UNION
    					SELECT LotHistory.LotId, LotHistory.PenId, IIF(LotHistory.Direction = 1, LotHistory.Head * -1, LotHistory.Head) AS Head
    						FROM scoFeedlot.LotHistory						
    						WHERE CAST(LotHistory.MovementDate AS DATE) > '2017-7-27' AND LotHistory.IsPending = 0 AND LotHistory.IsCancelled = 0
    				) A GROUP BY Lotid, PenId
    
    Select Distinct [scoFeedlot].[Pen].PenName,[scoFeedlot].LotMaster.Lotnumber,[scoFeedlot].LotPen.HeadCountInPen
    from 
    #LotPenAlloctionListTempTable
My C# code 
 

    public async Task<int> GetHeadCountInPenLotPens(DateTimeOffset date)
            {
                var lotPens = await _context.LotPens.ToListAsync();
                var lotHistory = await _context.LotHistories.ToListAsync();
            }
