从Dictionary对象列表中查找值(Finding a value from within a List of Dictionary objects)
我有可能是一个非常大的Dictionary对象列表,我需要从键中找到一个特定的值。 我当然可以这样做
foreach(Dictionary<long, string> t in Foo) { if (t.TryGetValue(key, out name)) break; }
这将很高兴地遍历Foo,直到它找到密钥或foreach结束。
为了加快速度,我宁愿使用少量的LINQ。 如果这是一个普通的列表,我会没事的,但由于这是一个Dictionary对象列表,我不太清楚它是如何完成的。
帮助或建议表示赞赏。
I have what can potentially be a very large list of Dictionary objects that I will need to find a particular value from a key. I can certainly do something like
foreach(Dictionary<long, string> t in Foo) { if (t.TryGetValue(key, out name)) break; }
and this will happily iterate through Foo until it finds the key or the foreach ends.
To speed this up, I'd prefer to use a small amount of LINQ. If this was a normal list, I'd be ok, but as this is a List of Dictionary objects, I'm not too sure how it's done.
Help or advice appreciated.
原文:https://stackoverflow.com/questions/16425102
最满意答案
由于此问题已更新,表明您需要2个提醒。
使用
if/elseif
块,块将在第一次匹配时完成。 所以在你的代码中,一旦匹配250
,if语句就完成了,而elseif
永远不会被评估。要获得独立执行的多个条件,它们应该是多个if语句:
$('#lblShowCounter').each(function () { var text = $(this).text(), matched; if (text >= '300') { alert('blink'); matched = true; } if (text >= '250') { alert('red'); matched = true; } if (!matched) { alert('nothing'); } });
Since the question has since been updated to indicate you want 2 alerts.
With an
if/elseif
block, the block will complete once it hits the first match. So in your code, once it matches250
the if statement completes and theelseif
is never evaluated.To get multiple conditions to execute independently, they should be multiple if statements:
$('#lblShowCounter').each(function () { var text = $(this).text(), matched; if (text >= '300') { alert('blink'); matched = true; } if (text >= '250') { alert('red'); matched = true; } if (!matched) { alert('nothing'); } });
相关问答
更多-
PHP其他条件不起作用(PHP else condition not working)[2023-06-16]
尝试这个: if (isset($somevariable) && $somevariable === 1) { echo 'Do Something here'; } else { echo 'Do Something Else'; } 这将检查变量是否已设置,以及是否在值和类型 (整数)中严格等于1 。 编辑:我试过你的代码,它似乎工作正常。 我建议你发布你的实际代码。 我认为问题不在于你的陈述。 Try this: if (isset($somevari ... -
由于此问题已更新,表明您需要2个提醒。 使用if/elseif块,块将在第一次匹配时完成。 所以在你的代码中,一旦匹配250 ,if语句就完成了,而elseif永远不会被评估。 要获得独立执行的多个条件,它们应该是多个if语句: $('#lblShowCounter').each(function () { var text = $(this).text(), matched; if (text >= '300') { alert('blink'); ...
-
您正在将boolean( loginStatus )与string进行比较。 删除引号,例如loginStatus == true ,并使用===而不是== 。 您也可以执行if(this.state.loginStatus) 。 You are comparing boolean (loginStatus) to string. Remove quotes, e.g. loginStatus == true and also use === instead of ==. You can also just ...
-
您的变量x不包含空字符串,但包含一些空格。 尝试使用trim()函数删除这些符号: if(x.trim() == ''){ ... } Your variable x contains not empty string but some spaces. Try to use trim() function to remove these symbols: if(x.trim() == ''){ ... }
-
试试这个..希望它有效 if(response.toString().trim().equalsIgnoreCase("Granted")){ runOnUiThread(new Runnable() { public void run() { Toast.makeText(UserPage.this,"Granted successfully", Toast.LENGTH_SHORT).show(); } }); In ...
-
“if”条件不起作用(The “if” condition not working)[2022-12-17]
你一定要阅读变量范围 。 这是一个正确的代码: NSString *name = [NSString stringWithFormat:@"Josh"]; NSString *greeting = nil; if ([name isEqualToString:@"Josh"]) { greeting = [NSString stringWithFormat:@"Hello Josh"]; } else { greeting = [NSString stringWithFormat:@"H ... -
如果条件不起作用(If condition is not working)[2022-03-21]
if语句不起作用,这是我看到的一个常见错误。 删除; 在这个条件声明中,它应该按预期工作: if (Main.inputLine.equals("1")); // <---- remove that ; { BGMusicmaindc(); } 注意 :您还可以使用简单的if语句(我的个人偏好): if (Main.inputLine.equals("1")) BGMusicmaindc(); This is a common error I see with if statements no ... -
如果条件不起作用(if condition not working)[2022-01-25]
重定向是否有效,没有if条件? 也许不是if条件不起作用,而是你的redirect-command中的一些错误。 这有时候有点奇怪,有header('Location: ...'); Does the Redirect work, without the if-condition? Maybe it's not the if-condition which is not working, but some mistake within your redirect-command. This sometime ... -
条件不起作用的地方(Where condition is not working)[2021-12-05]
一个可能的错误涉及使用AND和OR运算符,因为AND优先于OR ,并且您的查询可能以错误的方式解释。 所以你应该使用括号,写下这样的东西: SELECT `st_student`.`st_id`, `ab_date`, `as_date` `st_status` FROM `st_student` WHERE (`st_status` = 1 OR `st_status` = 2) AND (`ab_date` BETWEEN '08/01/2015' AND '08/31/2015' OR ... -
最有可能的问题是由于某些空格被添加到返回的字符串中而引起的。 您可以使用trim()删除它: $.post('edit_upload.php', formdata, function(data) { if (data.trim() == 'success') { console.log(data); } }); 你应该注意,通过JSON返回一个布尔值会更好的练习,而且更不容易出错,如下所示: $stmt = $db_conx->prepare('UPDATE tbl_uploads SET ...