-2

Before I set Url = r.Link I want to make sure r.link is not empty string?

Would I be able to do that in this code snippet?

target.RelatedArtifact = template.References.Select(r => new RelatedArtifact()
            {
                Type = r == template.References.First() ? RelatedArtifact.RelatedArtifactType.DerivedFrom : RelatedArtifact.RelatedArtifactType.Citation,
                Display = StripHtml(r.Text).Replace(" .", ".").Replace(" ®", "®") /* Html stripping artifact in <em></em> */,
                Url = r.Link
            }).ToList();

Output

<relatedArtifact>
    <type value="citation" />
    <display value="sss, et al. ss. s;2(2):2." />
    <url value="" /> shouldn't have this tag?
</relatedArtifact>
  • 2
    And what if `r.Link` is an empty string? – gunr2171 Apr 19 '19 at 19:59
  • Possible duplicate of [How can I check a C# variable is an empty string "" or null?](https://stackoverflow.com/questions/8224700/how-can-i-check-a-c-sharp-variable-is-an-empty-string-or-null) – Lews Therin Apr 19 '19 at 20:01

1 Answers1

0

very simply, test r.Link and replace it with whatever you need if is null or empty (or just white space, I assume)

target.RelatedArtifact = template.References.Select(r => new RelatedArtifact()
            {
                Type = r == template.References.First() ? RelatedArtifact.RelatedArtifactType.DerivedFrom : RelatedArtifact.RelatedArtifactType.Citation,
                Display = StripHtml(r.Text).Replace(" .", ".").Replace(" ®", "®") /* Html stripping artifact in <em></em> */,
                Url = string.IsNullOrWhiteSpace(r.Link)? SOME_MEANINGFUL_VALUE : r.Link
            }).ToList();
Gian Paolo
  • 4,161
  • 4
  • 16
  • 34