I want to translate SQL function to C# code.
I have a myCpRegs as a list from my own class, that is what I get from [fn_TrackingFromCpReg_CpRegPos] (@dtFrom, @dtTo)
function.
How to write this statement with a left outer join in EF Core?
insert into TrackingHistoryPos (TrackingHistoryID, Obj_ContID, Obj_TrayConcID, Obj_ProdID, Obj_CostCentreID, HasSN, Qty,
Parent_ContID, StockLocationIDFrom, StockLocationID,
IsDeleted, TrackTime, IsPreliminary, IsPhysical)
select
B.TrackingHistoryID, A.Obj_ContID, A.Obj_TrayConcID, A.Obj_ProdID, A.Obj_CostCentreID, A.HasSN, A.Qty,
A.Parent_ContID, A.StockLocationIDFrom, A.StockLocationID,
A.IsDeleted, A.TrackTime, A.IsPreliminary, A.IsPhysical
from
[fn_TrackingFromCpReg_CpRegPos] (@dtFrom, @dtTo) A
left outer join
TrackingHistory B on A.CpRegID = B.CpRegID
order by
A.CpRegPosID
This is what I tried to do
trackingHistoryPos = myCpRegs
.GroupJoin(trackings, c => c.CpRegID, t => t.CpRegID
myCpRegs.Where(x => (x.Pos_Obj_TrayConcID ?? x.Pos_Obj_ContID) is not null)
.Select(x => new TrackingHistoryPosRow()
{
TrackingHistoryID = , // TODO
Obj_ContID = x.Pos_Obj_ContID,
Obj_TrayConcID = x.Pos_Obj_TrayConcID,
Obj_ProdID = x.Pos_Obj_ProdID,
Obj_CostCentreID = , //TODO
HasSN = x.Pos_Obj_TrayConcID is not null && (x.Pos_Obj_TrayHasSN ?? false),
Qty = x.Pos_Qty,
Parent_ContID = , //TODO
StockLocationIDFrom = , //TODO
StockLocationID = , //TODO
IsDeleted = x.IsRejected == 1,
TrackTime = x.TrackTime,
IsPreliminary = x.IsPreliminary == 1,
IsPhysical = x.IsPhysical == 1,
})
).ToList();
I tried this solution and I become an error
var query = from c in myCpRegs
join t in trackingsHistory on c.CpRegID equals t.CpRegID into ps_jointable
from ct in ps_jointable.DefaultIfEmpty()
orderby c.Pos_CpRegPosID
select new TrackingHistoryPosRow()
{
TrackingHistoryID = ct.TrackingHistoryID, // TODO
Obj_ContID = c.Pos_Obj_ContID,
Obj_TrayConcID = c.Pos_Obj_TrayConcID,
Obj_ProdID = c.Pos_Obj_ProdID,
//Obj_CostCentreID = , //TODO
HasSN = c.Pos_Obj_TrayConcID is not null && (c.Pos_Obj_TrayHasSN ?? false),
Qty = c.Pos_Qty,
//Parent_ContID = , //TODO
//StockLocationIDFrom = , //TODO
//StockLocationID = , //TODO
IsDeleted = c.IsRejected == 1,
TrackTime = c.TrackTime,
IsPreliminary = c.IsPreliminary == 1,
IsPhysical = c.IsPhysical == 1,
};
trackingHistoryPos = query.ToList();