【问题标题】:Find() In a System.Collections.Generic.List<T>System.Collections.Generic.List<T> 中的 Find()
【发布时间】:2016-08-23 13:14:47
【问题描述】:

我正在使用 Telerik OrgChart 控件。我基本上在努力从节点中提取渲染字段。您可以将 OrgChartRenderedField 添加到每个节点;这有一个名为 Label 的属性。

每个节点的每个 OrgChartRenderedField 都包含在 OrgChartRenderedFieldCollection

public class OrgChartRenderedFieldCollection : System.Collections.Generic.List<OrgChartRenderedField>
Member of Telerik.Web.UI

它有一个名为 find() 的方法

public T Find(System.Predicate<T> match)
Member of System.Collections.Generic.List<T>

总结: 搜索与指定谓词定义的条件匹配的元素,并返回整个 System.Collections.Generic.List 中的第一个匹配项。

参数: match:定义要搜索的元素条件的 System.Predicate 委托。

返回: 与指定谓词定义的条件匹配的第一个元素(如果找到);否则,T 类型的默认值。

例外情况: System.ArgumentNullException:匹配为空。

我遇到的主要问题是我不知道如何在集合中搜索 Label = 'Some text' 的特定 OrgChartRenderedField 对象 欣赏它是第三方插件;希望知道对象的类型有人可以给我一个建议。 谢谢。

【问题讨论】:

  • 那个方法是List&lt;T&gt;的一部分,应该和list.Find(o =&gt; o.Label == "Some Text")一样简单
  • 请参阅this answer 了解谓词委托的说明。

标签: c# .net generics


【解决方案1】:

Find 方法是List&lt;T&gt; 的一部分。用法需要委托,最简单的方法可能是使用 lambda,如下所示:

var item = list.Find(o => o.Label == "Some Text");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 2021-11-04
    • 2021-09-06
    • 2022-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多