如何将附加参数传递给谓词?

发布于 2024-09-14 21:27:30 字数 271 浏览 3 评论 0原文

我的问题是通过传递特定日期从(日历)列表中查找确切的一个日历对象。我了解了谓词,但不确定是否向其传递参数。

colorcode 是列表(日历),日历类有一个名为 DtmDate 的属性,我想与它进行比较并返回所需的对象。

Dim a As Calendar = colourcode.Find(AddressOf New Calendar.FindByDate)

我从谷歌得到了谓词样本并达到了现在。但不知道如何将我的参数(即日期)传递给它。

My issue is to Find exact one Calendar object from a List Of(Calendar) by passing a particular date. I got to know about the predicate but not sure about passing parameter to it.

colorcode is List Of (Calendar) and calendar class has a property called DtmDate with which I want to compare and return the desired object.

Dim a As Calendar = colourcode.Find(AddressOf New Calendar.FindByDate)

I got the predicate samples from Google and reached till now. But not sure how to pass my parameter i.e. date to it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聆听风音 2024-09-21 21:27:30

您必须创建自己的谓词。您可以使用 lambda 来做到这一点,并通过将局部变量“提升”到其中,您可以对其进行参数化。下面是 .NET 3.5/Visual Studio 2008 的一个有点愚蠢的示例:

Dim lookFor As String = "e"
Dim predicate = Function(s as String) s.Contains(lookFor)

Dim list As New List(Of String)
list.Add("alfa")
list.Add("beta")
list.Add("gamma")
list.Add("delta")
Dim foundString As String = list.Find(predicate)

请注意如何更改 lookFor 的值以搜索其他字符串。

在 .NET 4/Visual Studio 2010 中,Visual Basic 具有更具表现力的 lambda 表达式:

You will have to create your own predicate. You can do that using a lambda and by "lifting" local variables into it you can parametrize it. Here is an somewhat silly example for .NET 3.5/Visual Studio 2008:

Dim lookFor As String = "e"
Dim predicate = Function(s as String) s.Contains(lookFor)

Dim list As New List(Of String)
list.Add("alfa")
list.Add("beta")
list.Add("gamma")
list.Add("delta")
Dim foundString As String = list.Find(predicate)

Notice how you can change the value of lookFor to search for other strings.

In .NET 4/Visual Studio 2010 Visual Basic has more expressive lambda expressions:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文