如何从列表对象中删除空字符串

时间:2019-04-19 19:58:29

标签: c# list

在设置Url = r.Link之前,我想确保r.link不是空字符串吗?

  

我可以在此代码段中做到这一点吗?

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();

输出

<relatedArtifact>
    <type value="citation" />
    <display value="sss, et al. ss. s;2(2):2." />
    <url value="" /> shouldn't have this tag?
</relatedArtifact>

1 个答案:

答案 0 :(得分:1)

非常简单,测试r.Link并将其替换为null或空(或者我假设只是空白)

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();