如何在 Umbraco 7 中获取具有布尔值 属性 的第一个父节点
How to get first parent node that has a boolean property in Umbraco 7
在部分视图宏中,我试图获取包含名为 "breakInheritance" 且值为 True
的 属性 的第一个祖先或自身节点。在搜索 SO 和 Our.Umbraco 论坛时,我一直在攻击这个语句/查询大约一个小时,但恐怕我没有任何进展。我觉得这应该很简单。
查询
var nodeToUse = CurrentPage.AncestorOrSelf(x => (x.HasProperty("breakInheritance") && x.GetPropertyValue<bool>("breakInheritance")));
lambda 表达式用红色下划线表示 - Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
我找到了大量关于这个错误的资源,但在我见过的所有情况下,这是他们可以编辑的自定义扩展方法,所以它并没有真正帮助我太多。
通过将 AncestorsOrSelf 方法转换为 IEnumerable<IPublishedContent>
,我能够获得所需的行为
var nodeToUse = ((IEnumerable<IPublishedContent>)CurrentPage
.AncestorsOrSelf())
.Where(x => (x.HasProperty("breakInheritance") && x.GetPropertyValue<bool>("breakInheritance") && x.HasValue("widgets")))
.FirstOrDefault();
我会说最好不要混合 strongly typed API with the dynamic API。在您的代码中,您可以执行 CurrentPage.AncestorsOrSelf
或 Model.Content.AncestorsOrSelf()
,第一个示例显然不接受 lambda 表达式,如错误消息所示。
请尝试以下操作:
var node = Model.Content.AncestorsOrSelf()
.FirstOrDefault(n => n.HasProperty("breakInheritance") &&
n.GetPropertyValue<bool>("breakInheritance"))
在部分视图宏中,我试图获取包含名为 "breakInheritance" 且值为 True
的 属性 的第一个祖先或自身节点。在搜索 SO 和 Our.Umbraco 论坛时,我一直在攻击这个语句/查询大约一个小时,但恐怕我没有任何进展。我觉得这应该很简单。
查询
var nodeToUse = CurrentPage.AncestorOrSelf(x => (x.HasProperty("breakInheritance") && x.GetPropertyValue<bool>("breakInheritance")));
lambda 表达式用红色下划线表示 - Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type
我找到了大量关于这个错误的资源,但在我见过的所有情况下,这是他们可以编辑的自定义扩展方法,所以它并没有真正帮助我太多。
通过将 AncestorsOrSelf 方法转换为 IEnumerable<IPublishedContent>
var nodeToUse = ((IEnumerable<IPublishedContent>)CurrentPage
.AncestorsOrSelf())
.Where(x => (x.HasProperty("breakInheritance") && x.GetPropertyValue<bool>("breakInheritance") && x.HasValue("widgets")))
.FirstOrDefault();
我会说最好不要混合 strongly typed API with the dynamic API。在您的代码中,您可以执行 CurrentPage.AncestorsOrSelf
或 Model.Content.AncestorsOrSelf()
,第一个示例显然不接受 lambda 表达式,如错误消息所示。
请尝试以下操作:
var node = Model.Content.AncestorsOrSelf()
.FirstOrDefault(n => n.HasProperty("breakInheritance") &&
n.GetPropertyValue<bool>("breakInheritance"))