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 项目 ...
随机推荐
- thinkphp对数据库操作有哪些内置函数
getModelName() 获取当前Model的名称 getTableName() 获取当前Model的数据表名称 switchModel(type,vars=array()) 动态切换模型 tab ...
- VBS基础篇 - 常量
常量:指的是在程序运行过程中其值保持不变的量,它用来保存固定不变的数值,字符串等常数 . 常量的定义:在vbscript中使用使用 Const 指令可以创建名称具有一定含义的字符串型或数值型常量,并给 ...
- 一点关于Ajax和一个等待图标的显示
一点关于Ajax和一个等待图标的显示 1.首先Ajax是asynchronous Java-Script and XML的简写.翻译过来就是异步的JS和XML. 2它的优点就是能不更新页面的情况下,得 ...
- 《C++Primer》复习——with C++11 [2]
1.数组引用形参,C++允许将变量定义成数组的引用,给予同样的道理,形参也可以是数组的引用,此时引用形参绑定到对应的实参上,也就是绑定到数组上 ]) { for (auto elem : arr) c ...
- spring mvc 学习笔记【1】---前言
扎扎实实地掌握好技术 ----------------------------------------------------------------------------------------- ...
- 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 ...
- 【UVA】【11762】Race to 1(得到1)
数学期望/马尔可夫过程 DP/记忆化搜索 刘汝佳老师白书上的例题…… //UVA 11762 #include<vector> #include<cstdio> #includ ...
- Matlab实现二进制矩阵转换为十进制
一.问题描述 [1 1 1 0 1 0 1 1 0 1 0 0 1 1 0] 每两位3转换为一个十进制数,共5列,那么转换后是ceil(5/3)=2列. [7 1 6 1 1 2] 二.问题分析 1. ...
- win7 安装Oracle 10G,11G
安装 10G : 安装说明: http://wenku.baidu.com/view/a73d048bd0d233d4b14e69a8.html 按这个安装成功过. 11G R2: 在Win7 6 ...
- 个人Android作品开发——FinancePad记账通
开发背景:针对在外工作的年轻一族,记录平时生活消费记录,方便清楚自己的钱花在哪些地方,方便管理. 开发时间:2013年7月中旬 开发环境:Eclipse Andorid SDK V2.0 开发语言:J ...