Well, you can change the return statement to:
return otherNameAndAddress ?? new AuditTrail { Name = "Default", Description = "Default };
or something like that ... but you say you want to assign different default values ββfor different calls. This means that you need to either pass the default value or default (for example, in the same way, using the operator with zero coalescing) on ββthe call site.
For instance:
public AuditTrail GetNamesAddressesEmployers(long registryId, int changedField, AuditField defaultValue) { var otherNameAndAddress = (from a in context.AuditTrails where a.ChangedField == changedField && a.RegistryId == registryId select a).FirstOrDefault(); return otherNameAndAddress && defaultValue; }
or save it as it is at present, and use it on the call site:
var auditTrail = GetNamesAddressesEmployers(registryId, changedField) ?? new AuditTrail { Name = "Foo", Description = "Bar" };
Itβs not entirely clear what is best based on your description.
EDIT: As Justin already mentioned, you can use DefaultIfEmpty (before FirstOrDefault ). This means that you need to pass the value, and not do it on the call site, but other than that they are very similar.
source share