ObjectSetting defaultSetting = new ObjectSetting();
defaultSetting.Value = "false";
defaultSetting.ObjectTypeSetting = db.ObjectSettings.First(ots => ots.Name == "PropName");
ObjectSetting testSetting = obj.ObjectSettings.DefaultIfEmpty(defaultSetting)
.FirstOrDefault(os => os.ObjectTypeSetting.Name == "PropName");
My final implementation will differ slightly, but my issue is that testSetting should be a populated object of type ObjectSetting with a value of “false” if the result of the linq expressions is empty, the result I’m getting is testSetting is null.
I’ve checked S/O for similar cases, i’ve also checked the msdn documentation and I feel I’m implementing right but obviously I’m not.
You’re using
FirstOrDefaultwith a predicate – so if yourobj.ObjectSettingsis not empty, but none of the matched values have anObjectTypeSetting.Nameof"PropName", you’ll still end up with no results.If you want to get your default setting in that case, you should use: