Check if KeyValuePair exists with LINQ's FirstOrDefault
http://stackoverflow.com/questions/793897/check-if-keyvaluepair-exists-with-linqs-firstordefault
问题:
I have a dictionary of type
Dictionary<Guid,int>
I want to return the first instance where a condition is met using
var available = m_AvailableDict.FirstOrDefault(p => p.Value == 0)
However, how do I check if I'm actually getting back a KeyValuePair? I can't seem to use != or == to check against default(KeyValuePair) without a compiler error. There is a similar thread here that doesn't quite seem to have a solution. I'm actually able to solve my particular problem by getting the key and checking the default of Guid, but I'm curious if there's a good way of doing this with the keyvaluepair. Thanks
If you just care about existence, you could use ContainsValue(0) or Any(p => p.Value == 0)instead? Searching by value is unusual for a Dictionary<,>; if you were searching by key, you could use TryGetValue.
One other approach:
var record = data.Where(p => p.Value == 1)
.Select(p => new { Key = p.Key, Value = p.Value })
.FirstOrDefault();
This returns a class - so will be null if not found.
Check if KeyValuePair exists with LINQ's FirstOrDefault的更多相关文章
- loadrunner录制时弹出invalid application path!please check if application exists对话框
问题:oadrunner录制时弹出invalid application path!please check if application exists对话框 原因:IE浏览器地址不对,需要手动重新选 ...
- [Javascript] Check both prop exists and value is valid
Sometime you need to check one prop exists on the object and value should not be ´null´ or ´undefine ...
- check process id exists
kill -0 pid sending the signal 0 to a given PID just checks if any process with the given PID is run ...
- 101个LINQ示例,包含几乎全部操作
Restriction Operators Where - Simple public void Linq1() { , , , , , , , , , }; var lowNums = from n ...
- 【2020-02-11】1346. Check If N and Its Double Exist
更多LeetCode解题详解 Easy Given an array arr of integers, check if there exists two integers N and M such ...
- 《果壳中的C# C# 5.0 权威指南》 - 学习笔记
<果壳中的C# C# 5.0 权威指南> ========== ========== ==========[作者] (美) Joseph Albahari (美) Ben Albahari ...
- 使用roslyn代替MSBuild完成解决方案编译
原本我是使用批处理调用 MSBuild 完成解决方案编译的,新版的 MSBuild 在 Visual Studio 2015 会自带安装. 当然在Visual Studio 2015 中,MSBuil ...
- petapoco-SQLServer模型增加注释
petapoco是个基于T4模板的轻量级ORM,好用效率高,具体介绍略了 获取注释基本原理是调用数据库::fn_listextendedproperty函数,获取扩展属性MS_Description ...
- T4 代码生成 Demo (抽奖程序)
参考自这位大狮的: https://github.com/Pencroff/Dapper-DAL/blob/master/Dapper-DAL/Models/ModelGenerator.tt 项目 ...
随机推荐
- 【js】js 让图片旋转
转http://www.cnblogs.com/ustcyc/p/3760116.html 核心: canvas.style.filter = "progid:DXImageTransfo ...
- vs2010旗舰版后,运行调试一个项目时调试不了,提示的是:无法使用“pc”附加到应用程序“webdev.webserver40.exe(PID:2260”
具体问题描述: vs2010旗舰版后,运行调试一个项目时调试不了,能编译,按ctrl+f5 可以运行,但是就是调试就不行,提示的是:无法使用“pc”附加到应用程序“webdev.webserver40 ...
- google api , the problem of null refresh token
http://stackoverflow.com/questions/10827920/google-oauth-refresh-token-is-not-being-received The ref ...
- nginx——内存池篇
nginx--内存池篇 一.内存池概述 内存池是在真正使用内存之前,预先申请分配一定数量的.大小相等(一般情况下)的内存块留作备用.当有新的内存需求时,就从内存池中分出一部分内存块,若内存块不够再继续 ...
- 简单制作mib表
今天放假后第一天上班,将假前自学制作mib表的东西说一下. 在这里呢,我以世界-中国-上海-闵行这种包含关系介绍,感觉更容易理解. MIB file的开始和结束 所有的MIB file的都以DEFIN ...
- 工程移除CocoaPods依赖库
http://zanderzhang.gitcafe.io/2015/09/26/工程移除CocoaPods依赖库/ 点这里--->CocoaPods安装和使用教程 当我们工程安装很多第三方开源 ...
- sqlserver 类似oracle的rownum功能: row_number
select row_number() over(order by t.id ) as num,id,name from (SELECT distinct [列 0] as id ,[列 1] as ...
- 2124: 等差子序列 - BZOJ
Description 给一个1到N的排列{Ai},询问是否存在1<=p1=3),使得Ap1,Ap2,Ap3,…ApLen是一个等差序列. Input 输入的第一行包含一个整数T,表示组数.下接 ...
- java多线程为什么要用while而不是if
对于java多线程的wait()方法,我们在jdk1.6的说明文档里可以看到这样一段话 从上面的截图,我们可以看出,在使用wait方法时,需要使用while循环来判断条件十分满足,而不是if,那么我们 ...
- C# mongodb [上]
概述 MongoDB是一个高性能,开源,无模式的文档型数据库,使用C++开发.是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是 ...