Linq左连接查询时,结果为空报错误Nullableobjectmusthaveavalue的解决方法
左连接查询结果除了主表字段不为空,其他的都有可能为空,可使用三元运算符代替Null值:
var TableNamelist = ctx.UWipprocconfset.Where(t => t.Factory == sFactory && t.AppliedEngineType == sAppliedEngineType && t.DeactiveFlag != "Y") .Join(
ctx.UWipprocconfsetitemrel.Where(t => t.Oper == sOper),
a => new { f = a.Factory, id = a.SetId },
b => new { f = b.Factory, id = b.SetId },
(a, b) => new
{
b.SeqNum,
b.ItemId,
b.Factory
}).Join(
ctx.UWipprocconfitem.Where(t => t.ActivityType == "P"),
a => new { f = a.Factory, id = a.ItemId },
b => new { f = b.Factory, id = b.ItemId },
(a, b) => new { a.SeqNum, BB = b }
).GroupJoin(
ctx.UWipprocconfsts.Where(t => t.EngineNo == sEngineNo),
a => new { f = a.BB.Factory, id = a.BB.ItemId },
b => new { f = b.Factory, id = b.ItemId },
(a, b) => new { AAA = a, BBB = b })
.SelectMany(
ab => ab.BBB.DefaultIfEmpty(),//Left Join
(a, b) => new
{
a.AAA.SeqNum,
a.AAA.BB.ActivityDesc,
a.AAA.BB.ItemId,
Factory = b.Factory == null ? " " : b.Factory,
EngineNo = b.EngineNo == null ? " " : b.EngineNo,
Oper = b.Oper == null ? " " : b.Oper,
Step = b.Step == null ? " " : b.Step,
ActivityType = b.ActivityType == null ? " " : b.ActivityType,
Shift = b.Shift == null ? " " : b.Shift,
NoOfWorker = b.NoOfWorker == null ? 0 : b.NoOfWorker,
Workers = b.Workers == null ? " " : b.Workers,
ActualStartDate = b.ActualStartDate == null ? " " : b.ActualStartDate,
ActualEndDate = b.ActualEndDate == null ? " " : b.ActualEndDate,
IssueFlag = b.IssueFlag == null ? " " : b.IssueFlag,
DelayFlag = b.DelayFlag == null ? " " : b.DelayFlag,
Remark = b.Remark == null ? " " : b.Remark,
CreateUserId = b.CreateUserId == null ? " " : b.CreateUserId,
CreateTime = b.CreateTime == null ? " " : b.CreateTime,
UpdateUserId = b.UpdateUserId == null ? " " : b.UpdateUserId,
UpdateTime = b.UpdateTime == null ? " " : b.UpdateTime
}).ToList();
以上是 Linq左连接查询时,结果为空报错误Nullableobjectmusthaveavalue的解决方法 的全部内容, 来源链接: utcz.com/z/512319.html