Null check in where?

问题描述 投票:0回答:1

我有以下内容:

var tagId = "5288";
source.Where(p => p.GetPropertyValue<IEnumerable<string>>("picker").Contains(tagId));

这将返回错误System.ArgumentNullException: Value cannot be null.

因此,某些返回的结果不包含选择器值。在上述声明中,我将如何检查?

这是Upbraco多节点树选择器,它是“选择器”值。

c# umbraco
1个回答
5
投票

[如果我理解正确,如果找不到GetPropertyValue值,则null的结果可以是picker。在这种情况下,您可以使用null conditional operator

source.Where(p => p.GetPropertyValue<IEnumerable<string>>("picker")?.Contains(tagId) == true);

注意?.之后的GetPropertyValue。如果该方法返回null,则不是true,因此这些将不会包含在过滤的对象中。

© www.soinside.com 2019 - 2024. All rights reserved.