每个循环不跳过项目 [英] For each loop not skipping items

查看:116
本文介绍了每个循环不跳过项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在vb.net中的每个循环,对于这个特定的例子,在列表中有2个项目,但在第一个项目之后循环退出是在代码中有错误

< pre $> $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $' b对于每个kvp在列表中
如果(kvp.Value = value)那么
id = kvp.Key
End If
Next
返回ID
End函数


解决方案

试试这个:

  dim kvp as KeyValuePair 
kvp = list.Find(p => p.Value = value))
if kvp = null然后返回否则返回kvp.Key

一位用户告诉我以这种方式修改它:

  dim kvp = list.Find(Function(e)e.Value = value)
如果kvp是Nothing然后返回否则返回kvp.Key

对不起代码有一些错误,但我不能尝试,我通常写在C#。
所以我的代码(在C#中)将是:

pre > KeyValuePair kvp = list.Find(p => p.Value == value));
返回kvp == null? :kvp.Key;


I have a for each loop in vb.net for this particular example there are 2 items in list but after the first item the loop exits are there errors in the code

Public Function findUserID(ByVal list As List(Of KeyValuePair(Of String, String)), ByVal value As String)
    Dim id As String = String.Empty
    For Each kvp  In list
        If (kvp.Value = value) Then
            id = kvp.Key
        End If
    Next
    Return id
End Function

解决方案

Try to use this:

dim kvp as KeyValuePair
kvp = list.Find(p=>p.Value = value))
if kvp = null then return "" else return kvp.Key

One user told me to modify it in this way:

dim kvp = list.Find(Function(e) e.Value = value)
If kvp Is Nothing Then Return "" Else Return kvp.Key

Sorry if this code has some error, but I cannot try and I usually write in C#.
So my code (in C#) would be:

KeyValuePair kvp = list.Find(p=>p.Value == value));
return kvp == null ? "" : kvp.Key;

这篇关于每个循环不跳过项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆