Linq扩展方法等效于VB.NET中的代码

时间:2014-05-01 22:19:19

标签: .net vb.net linq

,我想知道是否有办法在该代码中获得相同的结果:

For Each win As Window In Application.Current.Windows
    If win.GetType() = GetType(MainWindow) Then
       Return win
    End If
Next

我尝试使用,.Where方法,但它只是disnible por IEnumerable接口...我试图用.Cast(Of ...)投射它,但我不知道我必须使用哪种类型..

那么,有人可以帮助我吗?

提前致谢。

1 个答案:

答案 0 :(得分:3)

Return Application.Current.Windows.OfType(Of MainWindow)().FirstOrDefault()

如果找不到匹配项,它将返回Nothing。您可以将FirstOrDefault()结果分配给变量,如果Nothing不是Dim mainWindow = Application.Current.Windows.OfType(Of MainWindow)().FirstOrDefault() If mainWindow IsNot Nothing Then Return mainWindow End If ,则只返回当前代码。

{{1}}
相关问题