- The first 67 elements are always present.
- The last 2 elements are present if it is not null/empty
- The elements are always in the same order.
private string ConstructSeedReference(SeedViewModel model)
{
var reference = string.Concat(
model.Customer.CustomerCode.TrimEnd(),
model.Seed.CollectionCodeId.TrimEnd(),
model.Seed.SpeciesId.TrimEnd(),
model.Seed.Zone.TrimEnd(),
model.Seed.Elevation.TrimEnd(),
model.Seed.ColYear.TrimEnd(),
model.Seed.OrchardId);
if (!string.IsNullOrEmpty(model.Seed.GID))
{
reference += model.Seed.GID;GID.TrimEnd();
}
if (!string.IsNullOrEmpty(model.Seed.Clone))
{
reference += model.Seed.Clone;Clone.TrimEnd();
}
return reference;
}
EDIT: Just reread your question, if your concern is only about the NullReferenceException. You can simply use null-coalescing operator ?.:
private string ConstructSeedReference(SeedViewModel model)
{
return string.Concat(
model.Customer.CustomerCode.TrimEnd(),
model.Seed.CollectionCodeId.TrimEnd(),
model.Seed.SpeciesId.TrimEnd(),
model.Seed.Zone.TrimEnd(),
model.Seed.Elevation.TrimEnd(),
model.Seed.ColYear.TrimEnd(),
model.Seed.OrchardId,
model.Seed.GID?.TrimEnd(),
model.Seed.Clone?.TrimEnd());
}