首页 \ 问答 \ 从Dictionary对象列表中查找值(Finding a value from within a List of Dictionary objects)

从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
更新时间:2022-07-09 15:07

最满意答案

由于此问题已更新,表明您需要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 matches 250 the if statement completes and the elseif 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');
    }
});

相关问答

更多
  • 尝试这个: 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 ...
  • 你一定要阅读变量范围 。 这是一个正确的代码: NSString *name = [NSString stringWithFormat:@"Josh"]; NSString *greeting = nil; if ([name isEqualToString:@"Josh"]) { greeting = [NSString stringWithFormat:@"Hello Josh"]; } else { greeting = [NSString stringWithFormat:@"H ...
  • 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条件? 也许不是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 ...
  • 一个可能的错误涉及使用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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)