We have a complicated scenario where any of three tables, QRKeys, Tags and Docs, might or might not have data.
If Docs has data, then Devices does too.
We have it working as an inner join.
Now we're trying to extend it to a GroupJoin to handle missing tags.
Would upgrading to EF Core 7 help?
public static DeviceSummaryModel? GetDeviceSummary(string qrKey)
{
return context.QRKeys
.GroupJoin(context.Tags, qr => qr.TagID,
tg => tg.ID, (qr, tg) => new { qr, tg })
.Join(context.Documents, qt => qt.qr.DocID, dc => dc.ID, (qt, dc) => new { qt.qr, qt.tg, dc })
.Join(context.Devices, qtd => qtd.dc.DeviceID, dv => dv.ID, (qtd, dv) => new { qtd.qr, qtd.tg, dv })
.Where(x => x.qr.QRKey == qrKey)
.SelectMany(x => x.tg.DefaultIfEmpty(new TagModel()),
(x, tg) => new DeviceSummaryModel
{
QRKey = x.qr.QRKey,
TagNumber = tg.TagNumber,
Type = x.dv.Type,
Vendor = x.dv.Vendor,
Model = x.dv.Model
})
.FirstOrDefault();
}
System.InvalidOperationException : The LINQ expression 'DbSet() .GroupJoin( inner: DbSet(), outerKeySelector: qr => qr.TagID, innerKeySelector: tg => tg.ID, resultSelector: (qr, tg) => new { qr = qr, tg = tg })' could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable'
Working version for inner join:
return context.QRKeys
.Join(context.Tags, qr => qr.TagID, tg => tg.ID, (qr, tg) => new { qr, tg })
.Join(context.Documents, qt => qt.qr.DocID, dc => dc.ID, (qt, dc) => new { qt.qr, qt.tg, dc })
.Join(context.Devices, qtd => qtd.dc.DeviceID, dv => dv.ID, (qtd, dv) => new { qtd.qr, qtd.tg, dv })
.Where(x => x.qr.QRKey == qrKey)
.Select(x => new DeviceSummaryModel
{
QRKey = x.qr.QRKey, // or m.ppc.pc.ProdId
TagNumber = x.tg.TagNumber,
Type = x.dv.Type,
Vendor = x.dv.Vendor,
Model = x.dv.Model
})
.FirstOrDefault();