linq 帮助排除空项
linq help to exclude null items
我有下面的 linq 表达式,它派生出网页上的所有电子邮件标签。然而,它似乎也在捕获空锚 缺少 href 属性
项并抛出空引用异常错误:
{"Value cannot be null.\r\nParameter name: source"}
> var emailNodes =
> _htmlDocument.Value.DocumentNode.SelectNodes("//a[@href]")
> .Select(a => a.Attributes["href"].Value)
> .Where(href => href.StartsWith("mailto:")) // keep emails, skipp links
> .ToList();
LINQ 是否可以进行调整以完全排除空值?
EDIT: as noted, the error is seem when the html is lacking the href
all together. is there a way to handle for this in LINQ?**
检查没有 href 的标签,然后在使用点运算符之前检查 null 或空 href 字符串。
var emailNodes =
_htmlDocument.Value.DocumentNode.SelectNodes("//a[@href]")
.Where(a => a.Attributes["href"] != null)
.Select(a => a.Attributes["href"].Value)
.Where(href => !String.IsNullOrEmpty(href) && href.StartsWith("mailto:")) // keep emails, skipp links
.ToList();
我有下面的 linq 表达式,它派生出网页上的所有电子邮件标签。然而,它似乎也在捕获空锚 缺少 href 属性
项并抛出空引用异常错误:
{"Value cannot be null.\r\nParameter name: source"}
> var emailNodes =
> _htmlDocument.Value.DocumentNode.SelectNodes("//a[@href]")
> .Select(a => a.Attributes["href"].Value)
> .Where(href => href.StartsWith("mailto:")) // keep emails, skipp links
> .ToList();
LINQ 是否可以进行调整以完全排除空值?
EDIT: as noted, the error is seem when the html is lacking the href all together. is there a way to handle for this in LINQ?**
检查没有 href 的标签,然后在使用点运算符之前检查 null 或空 href 字符串。
var emailNodes =
_htmlDocument.Value.DocumentNode.SelectNodes("//a[@href]")
.Where(a => a.Attributes["href"] != null)
.Select(a => a.Attributes["href"].Value)
.Where(href => !String.IsNullOrEmpty(href) && href.StartsWith("mailto:")) // keep emails, skipp links
.ToList();